-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
137 lines (118 loc) · 4.11 KB
/
main.py
File metadata and controls
137 lines (118 loc) · 4.11 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import tensorflow.keras
from PIL import Image, ImageOps
import numpy as np
import cv2 as cv #Please install with PIP: pip install cv2
TM_DATA = None
model = None
cap = None
ret = None
frame = None
PredictionVariable = None
key = None
print('START')
# Disable scientific notation for clarity
np.set_printoptions(suppress=True)
# Load the model
model = tensorflow.keras.models.load_model('keras_model.h5')
# Create the array of the right shape to feed into the keras model
# The 'length' or number of images you can put into the array is
# determined by the first position in the shape tuple, in this case 1.
TM_DATA = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
def imgDetection():
img = cv.imread('bottle1.jpg')
while True:
img = cv.resize(img, (224, 224))
image_array = np.asarray(img)
normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
TM_DATA[0] = normalized_image_array
PredictionVariable = model.predict(TM_DATA)
print('Prediction:')
PredictionVariable = PredictionVariable * 100
print(PredictionVariable)
#Converting to one dimensional array
PredictionVariable = PredictionVariable.ravel()
if ((PredictionVariable[0] > PredictionVariable[1]) and (PredictionVariable[0] > 50)):
print('fish')
elif ((PredictionVariable[1] > PredictionVariable[0]) and (PredictionVariable[1] > 50)):
print('plastic')
else:
print('no object detected')
cv.imshow('output', img)
key = cv.waitKey(0)
if key == (ord('q')):
break
cv.destroyAllWindows()
img.release()
#
# def vidDetection():
# cap = cv.VideoCapture("https://192.168.43.1:8080/video")
#
# cap.set(3, 640)
# cap.set(4, 480)
#
# while True:
# ret , frame = cap.read()
# cv.imshow('Window',frame)
# frame = cv.resize(frame, (224, 224))
# image_array = np.asarray(frame)
# # Normalize the image
# normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
# # Load the image into the array
# TM_DATA[0] = normalized_image_array
# PredictionVariable = model.predict(TM_DATA)
# print('Prediction:')
# PredictionVariable = PredictionVariable * 100
# print(PredictionVariable)
#
# #Converting to one dimensional array
# PredictionVariable = PredictionVariable.ravel()
#
# if ((PredictionVariable[0] > PredictionVariable[1]) and (PredictionVariable[0] > 50)):
# print('fish')
# elif ((PredictionVariable[1] > PredictionVariable[0]) and (PredictionVariable[1] > 50)):
# print('plastic')
# else:
# print('no object detected')
#
# key = cv.waitKey(2000)
# if key == (ord('q')):
# break
# cv.destroyAllWindows()
# cap.release()
def vidDetection():
cap = cv.VideoCapture(0)
while True:
ret , frame = cap.read()
cv.imshow('Window',frame)
frame = cv.resize(frame, (224, 224))
image_array = np.asarray(frame)
# Normalize the image
normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
# Load the image into the array
TM_DATA[0] = normalized_image_array
PredictionVariable = model.predict(TM_DATA)
print('Prediction:')
PredictionVariable = PredictionVariable * 100
print(PredictionVariable)
#Converting to one dimensional array
PredictionVariable = PredictionVariable.ravel()
if ((PredictionVariable[0] > PredictionVariable[1]) and (PredictionVariable[0] > 50)):
print('fish')
elif ((PredictionVariable[1] > PredictionVariable[0]) and (PredictionVariable[1] > 50)):
print('plastic')
else:
print('no object detected')
key = cv.waitKey(2000)
if key == (ord('q')):
break
cv.destroyAllWindows()
cap.release()
option = input("1 or 2")
print("\n")
if option == '1':
imgDetection()
elif option == '2':
vidDetection()
else:
print('invalid option')
print('TNE END')