-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataSet copy.py
61 lines (46 loc) · 1.79 KB
/
dataSet copy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import os
import cv2
import numpy as np
image_folder_norm = '/home/zty/project/disease/handNorm'
image_folder_dise = '/home/zty/project/disease/handDise'
output_folder = '/home/zty/project/disease/dataset_b'
# Define the size of images
IMG_WIDTH = 224
IMG_HEIGHT = 224
# Define the number of images in each set
N = 50
# Define the output file names
train_file = 'train.npy'
test_file = 'test.npy'
# Collect the list of image paths
image_paths_norm = [os.path.join(image_folder_norm, f) for f in os.listdir(image_folder_norm) if f.endswith('.jpg')]
image_paths_dise = [os.path.join(image_folder_dise, f) for f in os.listdir(image_folder_dise) if f.endswith('.jpg')]
# Shuffle the image paths
np.random.shuffle(image_paths_norm)
np.random.shuffle(image_paths_dise)
# Select the first N images from each set
image_paths_norm = image_paths_norm[:N]
image_paths_dise = image_paths_dise[:N]
# Combine the image paths
image_paths = image_paths_norm + image_paths_dise
# Define the labels (0 for normal and 1 for diseased)
labels = np.zeros((2*N,), dtype=np.int32)
labels[N:] = 1
# Shuffle the images and labels
idxs = np.arange(2*N)
np.random.shuffle(idxs)
image_paths = [image_paths[i] for i in idxs]
labels = labels[idxs]
# Create the output folder if it does not exist
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# Initialize the data array
data = np.zeros((2*N, IMG_HEIGHT, IMG_WIDTH, 3), dtype=np.uint8)
# Load the images and resize them
for i, path in enumerate(image_paths):
img = cv2.imread(path)
img = cv2.resize(img, (IMG_WIDTH, IMG_HEIGHT))
data[i] = img
# Save the data to the output files
np.save(os.path.join(output_folder, train_file), {'images': data[:99], 'labels': labels[:99]})
np.save(os.path.join(output_folder, test_file), {'images': data[99:], 'labels': labels[99:]})