-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.py
97 lines (75 loc) · 2.78 KB
/
main.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
import sys
import Update
from ReverseSearch import UploadImage, SearchImage
import numpy as np
import cv2
import face_recognition
import json
ui = UploadImage()
si = SearchImage()
# link = ui.upload("rekha.jpeg")
# link
known_face_names, known_face_encodings = Update.update()
faceCascade = cv2.CascadeClassifier('Models/haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
cap.set(3,1000) # set Width
cap.set(4,720) # set Height
while True:
ret, img = cap.read()
# img = cv2.flip(img)
img = np.array(img)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor = 1.2,
minNeighbors = 8,
minSize = (10, 10)
# maxSize = (480,480)
)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),3)
roi_gray = gray[y:y+h+1, x:x+w+1]
roi_color = img[y:y+h+1, x:x+w+1]
cv2.imwrite("catch.jpg", roi_color)
# Load an image with an unknown face
# unknown_image = face_recognition.load_image_file("catch.jpg")
# Find all the faces and face encodings in the unknown image
unknown_image = roi_color
# unknown_image = roi_gray
# print(type(face_image))
# face_locations = face_recognition.face_locations(unknown_image)
face_encodings = face_recognition.face_encodings(unknown_image)
if(len(face_encodings) >= 1):
face_encodings = face_encodings[0]
# print(face_encodings)
# print(known_face_encodings)
matches = face_recognition.compare_faces(known_face_encodings, face_encodings, tolerance=0.62)
name = "Unknown"
face_distances = face_recognition.face_distance(known_face_encodings, face_encodings)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
name = known_face_names[best_match_index]
# font
font = cv2.FONT_HERSHEY_SIMPLEX
# org
org = (x , y + h + 40)
# fontScale
fontScale = 1.2
# Blue color in BGR
color = (255, 255, 150)
# Line thickness of 2 px
thickness = 2
# Using cv2.putText() method
image = cv2.putText(img, name, org, font,
fontScale, color, thickness, cv2.LINE_AA)
cv2.imshow('video',img)
k = cv2.waitKey(30) & 0xff
if k == 27: # press 'ESC' to quit
break
elif k == ord('f'):
link = ui.upload("catch.jpg")
data = si.search(link)
with open("temp.json", 'w') as f:
f.write(json.dumps(data, indent = 4))
cap.release()
cv2.destroyAllWindows()