-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfacetest.py
More file actions
238 lines (203 loc) · 7.88 KB
/
facetest.py
File metadata and controls
238 lines (203 loc) · 7.88 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
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
226
227
228
229
230
231
232
233
234
235
236
237
#change back to python 3.6 later (only changes involve urllib)
import requests
import json
import urllib.request, urllib.parse, urllib.error
import cv2
from azure.storage.blob import ContentSettings
from azure.storage.blob import BlockBlobService
import imagerecognition
import face_pay
#import urllib, urllib2
#hard coded values
key = "1f3021aa1ab74cedaf685826f631ab5a"
headers= {"Host": 'westcentralus.api.cognitive.microsoft.com', "Content-Type":'application/json','Ocp-Apim-Subscription-Key': key }
personGroupId = "test123"
#create person group
def createPersonGroup():
url = "https://westcentralus.api.cognitive.microsoft.com/face/v1.0/persongroups/"+ personGroupId
#not used elsewhere
personGroupDisplayName = "My Group"
body = { "name": personGroupDisplayName }
response = requests.put(url=url,json=body,headers=headers)
#delete person group removes everything related to it
def deletePersonGroup():
url = "https://westcentralus.api.cognitive.microsoft.com/face/v1.0/persongroups/" + personGroupId
response = requests.delete(url=url,headers=headers)
#takes in the user's id, gets their name, and adds a photo from an url
def addFace(personID):
name = getPersonName(personID)
if(name == 'Kaushik Tandon'):
photo = 'https://media.licdn.com/dms/image/C5103AQF6o6kmZyN5qQ/profile-displayphoto-shrink_200_200/0?e=1525255200&v=alpha&t=qSE3eKdrVZkrpMpWnS9ldheYY7t0NF1E6d2wbkL3ig8'
elif(name == 'Radhika Agrawal'):
photo = 'https://scontent-lax3-1.xx.fbcdn.net/v/t31.0-8/22859851_833930706775284_2298164206331624972_o.jpg?oh=da14e9f5d3f6dd67ed16ac6b5d49ca23&oe=5B49CFC2'
elif(name == 'Maegan Chew'):
photo = 'https://scontent-lax3-1.xx.fbcdn.net/v/t31.0-8/18839535_710270525842729_6235509578421077480_o.jpg?oh=812bc4ca650131295a23e089e02c7f3b&oe=5B442168'
elif(name == 'Chris Hailey'):
photo = 'https://scontent-lax3-1.xx.fbcdn.net/v/t31.0-8/21457362_1762418087392430_5728002921223690541_o.jpg?oh=1e62faa1f514bef393fb4a5e5cf3830d&oe=5B4BFA1C'
else:
captureImageToBlob()
photo = 'https://chrishacktech.blob.core.windows.net/photos/newuser_blob.jpg'
url = "https://westcentralus.api.cognitive.microsoft.com/face/v1.0/persongroups/"+personGroupId+"/persons/"+personID+"/persistedFaces"
data = {"url":photo}
requests.post(url=url,json=data,headers=headers)
#create person group person (including faces). returns list of ids of created people
def createPerson(names):
url = "https://westcentralus.api.cognitive.microsoft.com/face/v1.0/persongroups/"+personGroupId+"/persons"
#hardcoded names
#names = ["Kaushik", "Radhika", "Maegan", "Chris"]
ids = []
for name in names:
body = { "name": name }
response = requests.post(url=url,json=body,headers=headers)
try:
tempID = str(response.json()["personId"])
except:
print("createPerson rate limited")
ids.append(tempID)
addFace(tempID)
return ids
#deletes a person from their person id
def deletePerson(personID):
url = "https://westcentralus.api.cognitive.microsoft.com/face/v1.0/persongroups/"+personGroupId+"/persons/"+personID
response = requests.delete(url=url,headers=headers)
def getPersonName(personID):
getURL = "https://westcentralus.api.cognitive.microsoft.com/face/v1.0/persongroups/" + personGroupId + "/persons/" + personID
response = requests.get(url=getURL,headers=headers)
try:
name = response.json()["name"]
except:
print("getPersonName rate limited")
return name
def trainGroup():
url = "https://westcentralus.api.cognitive.microsoft.com/face/v1.0/persongroups/"+personGroupId+"/train"
response = requests.post(url=url, headers=headers)
def detectFace(imageUrl):
localHeaders = {"Host": 'westcentralus.api.cognitive.microsoft.com', "Content-Type":'application/octet-stream','Ocp-Apim-Subscription-Key': key }
urlAPI = 'https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect?' + urllib.parse.urlencode({ 'returnFaceId': 'true'})
#photo to check
data = open(imageUrl, 'rb').read()
#body = { "url" : imageUrl}
response = requests.post(url = urlAPI, data = data, headers = localHeaders)
#print (response.json())
try:
theirID = response.json()[0]["faceId"]
except:
print("rate limits suck")
faceIDs = [theirID]
identifyURL = 'https://westcentralus.api.cognitive.microsoft.com/face/v1.0/identify'
body = {"personGroupId":personGroupId,"faceIds":faceIDs,"maxNumOfCandidatesReturned": 1,"confidenceThreshold": 0.5}
response = requests.post(url = identifyURL, json = body, headers = headers)
try:
winner = response.json()[0]['candidates'][0]['personId']
except:
print("rate limited")
return getPersonName(winner)
def processItems(numIteScan):
numItemsScanned = int(numIteScan)
i = 1
items = []
prices = []
while (i <= numItemsScanned):
cap = cv2.VideoCapture(0)
while (True):
ret, frame = cap.read()
if ret is True:
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
else:
continue
cv2.imshow('frame', rgb)
if cv2.waitKey(1) & 0xFF == ord('q'):
picName = 'item' + str(i) + '.jpg'
out = cv2.imwrite(picName, frame)
cap.release()
break
cv2.destroyAllWindows()
words = imagerecognition.rekognition(picName)
itemName, itemPrice = imagerecognition.walmartSearch(words)
items.append(itemName)
prices.append(itemPrice)
i = i + 1
return items, prices
#def getItem():
# cap = cv2.VideoCapture(0)
#
# while(True):
# ret, frame = cap.read()
# if ret is True:
# rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
# else:
# continue
# cv2.imshow('frame', rgb)
# if cv2.waitKey(1) & 0xFF == ord('q'):
# picName = 'blobItem.jpg'
# out = cv2.imwrite(picName, frame)
# cap.release()
# cv2.destroyAllWindows()
# break
# block_blob_service.create_blob_from_path(
# 'photos',
# picName,
# picName,
# content_settings=ContentSettings(content_type='image/jpg'))
#def itemDetect(imageUrl):
# handWriteDetectKey = "85f3d93125ad42b78b08b6a9e5c5f240"
# text_recognition_url = "https://westcentralus.api.cognitive.microsoft.com/vision/v1.0/RecognizeText"
# detectHeaders = {'Ocp-Apim-Subscription-Key': handWriteDetectKey}
# detectParams = {'handwriting' : True}
# detectData = {'url': imageUrl}
# response = requests.post(text_recognition_url, headers=detectHeaders, params=detectParams, json=detectData)
# response.raise_for_status()
# operation_url = response.headers["Operation-Location"]
# import time
# analysis = {}
# while not "recognitionResult" in analysis:
# response_final = requests.get(response.headers["Operation-Location"], headers=detectHeaders)
# analysis = response_final.json()
# time.sleep(1)
#val = [(line["boundingBox"], line["text"]) for line in analysis["recognitionResult"]["lines"]]
# val = [line["text"] for line in analysis["recognitionResult"]["lines"]]
# return val
def captureImage():
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
if ret is True:
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
else:
continue
cv2.imshow('frame', rgb)
if cv2.waitKey(1) & 0xFF == ord('q'):
picName = 'capture.jpg'
out = cv2.imwrite(picName, frame)
cap.release()
cv2.destroyAllWindows()
return picName
def captureImageToBlob():
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
if ret is True:
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
else:
continue
cv2.imshow('frame', rgb)
if cv2.waitKey(1) & 0xFF == ord('q'):
picName = 'newuser_blob.jpg'
out = cv2.imwrite(picName, frame)
block_blob_service = BlockBlobService(account_name='chrishacktech', account_key='xThYN0X/abcijoR3hiP/g8Wu7LgyyC9Skk9yVC+b27jMMYrK7ulMTq6ZeliaJhfJDkRl1pNJ+MD+Av9As9W5tw==')
block_blob_service.create_blob_from_path(
'photos',
picName,
picName,
content_settings=ContentSettings(content_type='image/jpg'))
cap.release()
cv2.destroyAllWindows()
break
def determineCost(arr):
return sum(arr)
def processReceipt(items, prices):
i = 0
while (i < len(items)):
print ("Item " + str(i+1) + ": " + items[i] + " " + str(prices[i]))
i = i + 1
print ("Total: " + str(sum(prices)))