-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.py
184 lines (168 loc) · 6.48 KB
/
camera.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import os
import cv2
from darkflow.net.build import TFNet
#from finding_lane_1 import detect_lanes_img
import numpy as np
#import serial
import time
import json
def draw_boxes_image(colors,results,frame):
#arduino = serial.Serial('COM3',9600)
json_file = []
for (color, result) in zip (colors, results):
#Convert confidence level to int from float
json_temp = dict(result)
json_conf = json_temp['confidence']
json_conf = int(round(json_conf*100))
json_temp['confidence'] = json_conf
#till here
json_file.append(json_temp)
tl = (result['topleft']['x'], result['topleft']['y'])
br = (result['bottomright']['x'], result['bottomright']['y'])
confidence = result['confidence']
label = result['label']
text = '{}: {:.1f}%'.format(label,confidence*100 )
frame = cv2.rectangle(frame, tl, br, color, 5)
frame = cv2.putText(frame, text, tl, cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0), 2)
'''
if (arduino.isOpen()):
word = label + '\n'
bword = bytes(word,'utf-8')
arduino.write(bword)
'''
cv2.imshow ("predicted",frame)
with open('data.json','w') as outfile:
json.dump(json_file,outfile)
#arduino.close()
'''
def send_to_arduino(word):
arduino = serial.Serial('COM3',9600)
if (arduino.isOpen()):
word = word +'\n'
bword = bytes(word,'utf-8')
arduino.write(bword)
def result_to_word(result):
word = '{}'.format(result['label'])
return word
'''
# M A I N P R O G R A M
options = {
'model': 'cfg/yolo-face.cfg',
'load': 'bin/yolo-face_final.weights',
'threshold': 0.3,
'gpu': 0.5
}
tfnet = TFNet(options)
fileRead = input('Enter Image/Video/Webcam : ')
filename , fileExtension = os.path.splitext(fileRead)
colors = [tuple(255 * np.random.rand(3)) for _ in range(10)]
if (fileExtension == '.jpg') :
frame = cv2.imread (fileRead)
# frame = detect_lanes_img(frame)
results = tfnet.return_predict(frame)
draw_boxes_image(colors,results,frame)
cv2.waitKey(0)
cv2.destroyAllWindows()
elif (fileExtension == '.mp4') :
capture = cv2.VideoCapture(fileRead)
capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
frame_number = 1
json_file = {}
while True:
stime = time.time()
ret, frame = capture.read()
results = tfnet.return_predict(frame)
if ret:
#frame = detect_lanes_img(frame)
json_frame = []
frame_str = 'frame {}'.format(frame_number)
for (color, result) in zip (colors, results):
#Convert confidence level to int from float
json_temp = dict(result)
json_conf = json_temp['confidence']
json_conf = int(round(json_conf*100))
json_temp['confidence'] = json_conf
json_frame.append(json_temp)
tl = (result['topleft']['x'], result['topleft']['y'])
br = (result['bottomright']['x'], result['bottomright']['y'])
confidence = result['confidence']
label = result['label']
coorX= result['topleft']['x']+result['bottomright']['x']/2
coorY= result['topleft']['y']+result['bottomright']['y']/2
centroids = (coorX,coorY)
text = '{}: {:.1f}%'.format(label,confidence*100 )
frame = cv2.rectangle(frame, tl, br, color, 5)
frame = cv2.putText(frame, text, tl, cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0), 2)
'''
if (arduino.isOpen()):
word = label + '\n'
bword = bytes(word,'utf-8')
arduino.write(bword)
'''
json_file[frame_str] = json_frame
frame_number = frame_number + 1
cv2.imshow ("predicted",frame)
print('FPS {:.1f}'.format(1 / (time.time() - stime)))
if cv2.waitKey(1) & 0xFF == ord('q'):
break
capture.release()
cv2.destroyAllWindows()
with open('data.json','w') as outfile:
json.dump(json_file,outfile)
print('Jumlah Frame :{}'.format(frame_number))
#arduino.close()
elif (fileExtension == '.0' or fileExtension == '.1' ) :
fileExtension = fileExtension[-1]
cam = int (fileExtension)
capture = cv2.VideoCapture(cam)
frame_number = 1
json_file = {}
px_cm_person = 20000
while True:
stime = time.time()
ret, frame = capture.read()
results = tfnet.return_predict(frame)
if ret:
#frame = detect_lanes_img(frame)
json_frame = []
frame_str = 'frame {}'.format(frame_number)
for (color, result) in zip (colors, results):
#Convert confidence level to int from float
json_temp = dict(result)
json_conf = json_temp['confidence']
json_conf = int(round(json_conf*100))
json_temp['confidence'] = json_conf
json_frame.append(json_temp)
tl = (result['topleft']['x'], result['topleft']['y'])
br = (result['bottomright']['x'], result['bottomright']['y'])
confidence = result['confidence']
label = result['label']
coorX= result['topleft']['x']+result['bottomright']['x']/2
coorY= result['topleft']['y']+result['bottomright']['y']/2
centroids = (coorX,coorY)
frame = cv2.rectangle(frame, tl, br, color, 5)
text = '{}: {:.1f}%'.format(label,confidence*100 )
frame = cv2.putText(frame, text, tl, cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0), 2)
'''
if (arduino.isOpen()):
word = label + '\n'
bword = bytes(word,'utf-8')
arduino.write(bword)
'''
json_file[frame_str] = json_frame
frame_number = frame_number + 1
cv2.imshow ("predicted",frame)
print('FPS {:.1f}'.format(1 / (time.time() - stime)))
if cv2.waitKey(1) & 0xFF == ord('q'):
break
capture.release()
cv2.destroyAllWindows()
with open('data.json','w') as outfile:
json.dump(json_file,outfile)
print('Jumlah Frame :{}'.format(frame_number))
#arduino.close()
elif (fileRead == 'exit'):
pass
else:
print('Input Invalid')