-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpure_CV.py
156 lines (131 loc) · 5.27 KB
/
pure_CV.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
import face_recognition as fr
import cv2 as cv
import pymongo
from pymongo import MongoClient
import base64
# client = MongoClient('')
#
# db = client["medar"]
# collection = db.test_collection
#
# faces_col = db["faces"]
# patient_col = db["patients"]
#
# users_col = db["users"]
# profile_col = db["profile"]
face_encodings = []
face_locations = []
face_names = []
known_face_encodings = []
known_face_names = []
encoded = ""
# the actual training and encoding
def addPerson(name):
# train cv to recognize face after it construct a set of vectors and then encodes it
# id = users_col.find(name)
# patient = profile_col.find(id)
# picture_link = patient.img
# picture = base64.decodestring(picture_link)
new_person_image = fr.load_image_file('shyam_pic.jpg')
new_person_encoding = fr.face_encodings(new_person_image)[0]
known_face_encodings.append(new_person_encoding)
encoded = new_person_encoding
known_face_names.append(name)
initialize_camera()
def initialize_camera():
found_name = ""
# this enables the video feed
vid_cap = cv.VideoCapture(0)
process_this_frame = True
name = "Unknown"
while True:
# take a single frame from the feed and then resize it to 1/4 off the size for faster processing
ret, frame = vid_cap.read()
small_frame = cv.resize(frame, (0, 0), fx=0.25, fy=0.25)
# a small frame to easily show that the computer recognizes someone
rgb_small_frame = small_frame[:, :, ::-1]
# Only process every other frame of video to save time
if process_this_frame:
# Find all the faces and face encodings in the current frame of video
face_locations = fr.face_locations(rgb_small_frame)
face_encodings = fr.face_encodings(rgb_small_frame, face_locations)
face_names = []
# for face_encoding in face_encodings:
# # See if the face is a match for the known face(s)
# matches = fr.compare_faces(known_face_encodings, face_encoding)
# name = "Unknown"
# medical_info = "N/A"
#
# counter = 0
# temp = False
# for i in matches:
# for j in i:
# if j == True:
# temp = True
# first_match = j
# break
# counter += 1
#
# # If a match was found within encoded_faces, just use the first one.
# if temp:
# first_match_index = counter
# name = known_face_names[first_match_index]
for face_encoding in face_encodings:
# See if the face is a match for the known face(s)
matches = fr.compare_faces(known_face_encodings, face_encoding)
name = "Unknown"
# If a match was found in known_face_encodings, just use the first one.
if True in matches:
first_match_index = matches.index(True)
name = known_face_names[first_match_index]
face_names.append(name)
process_this_frame = not process_this_frame # Display the results
for (top, right, bottom, left), name in zip(face_locations, face_names):
# Scale back up face locations since the frame we detected in was scaled to 1/4 size
top *= 4
right *= 4
bottom *= 4
left *= 4
cv.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
cv.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv.FILLED)
font = cv.FONT_HERSHEY_DUPLEX
cv.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
cv.imshow('Video', frame)
# exut by pressing 'q'
if cv.waitKey(1) & 0xFF == ord('q'):
break
# terminating the final processes
vid_cap.release()
cv.destroyAllWindows()
#id = users_col.find(name)
#patient = profile_col.find(id)
#return patient;
print(name)
def start():
# REGULAR VERSION WITHOUT THE POPUP#
print("Welcome to Medi-Vision")
print("Type in 1 if you'd like to add someone to the database or Type 2 initialize the camera: ")
func = int(input(""))
print(func)
if func == 1:
addPerson(input("Provide a name: "))
else:
initialize_camera()
# NOW WITH POPUP THAT INCLUDES THE BUTTONS
# tk = Tk()
# var = StringVar()
# label = Label(tk, textvariable=var, relief=RAISED)
#
# var.set("Welcome to Medi-Vision \n"+"Would you like to add someone or"
# "launch Medi-Vision", ) # What you want the text to be
# label.pack()
#
# tk.geometry("100x150") # The size of the box
# tk.wm_title("Insight") # The name of it
# b1 = Button(master, text="Starup Camera", command=initialize_camera())
# b2 = Button(master, text="Add New Person", command=addPerson())
# b1.pack(side = LEFT)
# b2.pack(side = RIGHT)
# tk.mainloop()
start()
#face_landmarks_list = fr.face_landmarks(image)