-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknn_webcam2.py
227 lines (155 loc) · 8.21 KB
/
knn_webcam2.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import math
from sklearn import neighbors
import os
import os.path
import pickle
from PIL import Image, ImageDraw
import face_recognition
from face_recognition.face_recognition_cli import image_files_in_folder
import cv2
from pymongo import MongoClient
from datetime import datetime
import numpy as np
import platform
#Step 1: Connect to MongoDB - Note: Change connection string as needed
client = MongoClient(port=27017)
db=client.db
# ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
temp=38
prevname="Unknown"
# pr=['top:10','right:40','bottom:20','left:10']
def running_on_jetson_nano():
# To make the same code work on a laptop or on a Jetson Nano, we'll detect when we are running on the Nano
# so that we can access the camera correctly in that case.
# On a normal Intel laptop, platform.machine() will be "x86_64" instead of "aarch64"
return platform.machine() == "aarch64"
def get_jetson_gstreamer_source(capture_width=1280, capture_height=720, display_width=1280, display_height=720, framerate=60, flip_method=0):
"""
Return an OpenCV-compatible video source description that uses gstreamer to capture video from the camera on a Jetson Nano
"""
return (
f'nvarguscamerasrc ! video/x-raw(memory:NVMM), ' +
f'width=(int){capture_width}, height=(int){capture_height}, ' +
f'format=(string)NV12, framerate=(fraction){framerate}/1 ! ' +
f'nvvidconv flip-method={flip_method} ! ' +
f'video/x-raw, width=(int){display_width}, height=(int){display_height}, format=(string)BGRx ! ' +
'videoconvert ! video/x-raw, format=(string)BGR ! appsink'
)
def predict(X_img_path, knn_clf=None, model_path=None, distance_threshold=0.6):
# if not os.path.isfile(X_img_path) or os.path.splitext(X_img_path)[1][1:] not in ALLOWED_EXTENSIONS:
# raise Exception("Invalid image path: {}".format(X_img_path))
if knn_clf is None and model_path is None:
raise Exception("Must supply knn classifier either thourgh knn_clf or model_path")
# Load a trained KNN model (if one was passed in)
if knn_clf is None:
with open(model_path, 'rb') as f:
knn_clf = pickle.load(f)
# Load image file and find face locations
# X_img = face_recognition.load_image_file(X_img_path)
X_face_locations = face_recognition.face_locations(X_img_path)
# If no faces are found in the image, return an empty result.
if len(X_face_locations) == 0:
return []
# Find encodings for faces in the test iamge
faces_encodings = face_recognition.face_encodings(X_img_path, known_face_locations=X_face_locations)
# Use the KNN model to find the best matches for the test face
closest_distances = knn_clf.kneighbors(faces_encodings, n_neighbors=1)
are_matches = [closest_distances[0][i][0] <= distance_threshold for i in range(len(X_face_locations))]
# Predict classes and remove classifications that aren't within the threshold
# return [(pred, loc,print(pred)) if rec else ("unknown", loc,print("unknown")) for pred, loc, rec in zip(knn_clf.predict(faces_encodings), X_face_locations, are_matches)]
return [(pred, loc) if rec else ("unknown", loc) for pred, loc, rec in zip(knn_clf.predict(faces_encodings), X_face_locations, are_matches)]
# for pred, loc, rec in zip(knn_clf.predict(faces_encodings), X_face_locations, are_matches):
# if rec:
# return (pred, loc)
# else:
# return ("unknown", loc)
# def show_prediction_labels_on_image(img_path, predictions):
pil_image = Image.open(img_path).convert("RGB")
draw = ImageDraw.Draw(pil_image)
draw1 =ImageDraw.Draw(pil_image1)
for name, (top, right, bottom, left) in predictions:
# Draw a box around the face using the Pillow module
draw.rectangle(((left, top), (right, bottom)), outline=(0, 0, 255))
# There's a bug in Pillow where it blows up with non-UTF-8 text
# when using the default bitmap font
temp=38
name=name+" Temp="+str(temp)+"c"
name = name.encode("UTF-8")
# Draw a label with a name below the face
text_width, text_height = draw.textsize(name)
draw.rectangle(((left, bottom - text_height - 10), (right, bottom)), fill=(0, 0, 255), outline=(0, 0, 255))
draw.text((left + 6, bottom - text_height - 5),name, fill=(255, 255, 255, 255))
# draw.text((left + 30, bottom - text_height - 30), temp, fill=(255, 255, 255, 255))
# left1=50
# bottom1=150
# right1=150
# #Draw a label for temperature
# text_width1, text_height1 = draw.textsize(temp)
# draw.rectangle(((left, bottom-text_width-10 ), (right, bottom)), fill=(0, 0, 255), outline=(0, 0, 255))
# draw.text((left + 6, bottom - text_height - 5), temp, fill=(255, 255, 255, 255))
# for temp, (top, right, bottom, left) in predictions:
# # Draw a box around the face using the Pillow module
# draw1.rectangle(((left, top), (right, bottom)), outline=(0, 0, 255))
# temp = temp.encode("UTF-8")
# text_width, text_height = draw.textsize(temp)
# draw1.rectangle(((left, bottom - text_height - 40), (right, bottom)), fill=(0, 255, 0), outline=(0, 0, 255))
# draw1.text((left + 6, bottom - text_height - 5), name, fill=(255, 255, 255, 255))
# Remove the drawing library from memory as per the Pillow docs
del draw
del draw1
# Display the resulting image
pil_image.show()
if __name__ == "__main__":
if running_on_jetson_nano():
# Accessing the camera with OpenCV on a Jetson Nano requires gstreamer with a custom gstreamer source string
video_capture = cv2.VideoCapture(0)
#video_capture = cv2.VideoCapture(get_jetson_gstreamer_source(), cv2.CAP_GSTREAMER)
else:
# Accessing the camera with OpenCV on a laptop just requires passing in the number of the webcam (usually 0)
# Note: You can pass in a filename instead if you want to process a video file instead of a live camera stream
video_capture = cv2.VideoCapture(0)
# video_capture = cv2.VideoCapture(0)
# current_time = datetime.datetime.now()
now = datetime.now()
date_time = now.strftime("%d/%m/%Y ; %H:%M:%S")
while True:
from temp import tempe
temp=tempe()
ret, frame = video_capture.read()
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Find all people in the image using a trained classifier model
# Note: You can pass in either a classifier file name or a classifier model instance
predictions = predict(X_img_path=rgb_frame, model_path="trained_knn_modelnew.clf")
pred=str(predictions)
K = ','
# using list comprehension + split()
# K Character Split String
res = [i for j in pred[2:].split(K) for i in (j, K)][:-1]
name=res[0]
if name!= prevname:
print(name)
prevname = name
if name == '':
name='Unknown'
attendance={
'date_time' : date_time,
'name' : name,
'temperature' : temp
}
result=db.seasattendance.insert_one(attendance)
print('printed {0}'.format(result.inserted_id))
for name, (top, right, bottom, left) in predictions:
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
name=name+" Temp="+str(temp)+"c"
# Draw a label with a name below the face
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
# Display the resulting image
cv2.imshow('Video', frame)
# Hit 'q' on the keyboard to quit!
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()