-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimg-recog.py
67 lines (57 loc) · 2.03 KB
/
img-recog.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
62
63
64
65
66
67
# This is a tentative script to help decide how the further img recognition will be carried out
# The reason why it is not being developed on is that I might switch direction and choose another diretion to differentiate between the two states than using img recognition.
import os
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.preprocessing.image import ImageDataGenerator
# Define the folders where your image data is stored
flex_dir = 'path/to/flex_images'
no_flex_dir = 'path/to/no_flex_images'
# Create an ImageDataGenerator to preprocess and augment your images
datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True,
fill_mode='nearest',
validation_split=0.2
)
# Load and preprocess images from the directories
image_size = (224, 224) # Adjust the size as needed
batch_size = 32
train_data = datagen.flow_from_directory(
flex_dir,
target_size=image_size,
batch_size=batch_size,
class_mode='binary',
subset='training'
)
validation_data = datagen.flow_from_directory(
flex_dir,
target_size=image_size,
batch_size=batch_size,
class_mode='binary',
subset='validation'
)
# Create a simple CNN model
model = keras.Sequential([
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(224, 224, 3)),
layers.MaxPooling2D(2, 2),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D(2, 2),
layers.Conv2D(128, (3, 3), activation='relu'),
layers.MaxPooling2D(2, 2),
layers.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dense(1, activation='sigmoid') # Binary classification
])
# Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Train the model
epochs = 10 # Adjust the number of epochs as needed
model.fit(train_data, epochs=epochs, validation_data=validation_data)
# Save the trained model
model.save('flex_vs_no_flex_model.h5')