-
Notifications
You must be signed in to change notification settings - Fork 0
/
vision.py
90 lines (62 loc) · 2.63 KB
/
vision.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
import requests
import base64
import os
from PIL import Image
import io
subscription_key = "[YOUR KEY]"
endpoint = "https://trafficsihthackatum19.cognitiveservices.azure.com/"
def crop_out_image(filename, x,w,y,h):
im = Image.open(r"[YOUR_PATH]" + filename)
crop = im.crop((x, y, x+w, y+h))
file_name = "temp2"
image_path = "[YOUR PATH]" + file_name
crop.save(image_path,'JPEG')
image_data = open(image_path, "rb").read()
headers = {'Ocp-Apim-Subscription-Key': subscription_key,
'Content-Type': 'application/octet-stream'}
params = {'visualFeatures': 'Description, Tags, Categories'}
analyze_url = endpoint + "vision/v2.1/analyze"
response = requests.post(analyze_url, headers=headers, params=params, data=image_data)
analysis = response.json()
for tag in analysis["tags"]:
if tag["name"] == "green":
os.remove("temp2.jpeg")
return True
os.remove("temp2.jpeg")
return False
def blob_to_image_converter(profile_image_blob):
if profile_image_blob is not None and len(profile_image_blob) > 0:
image = base64.b64decode(str(profile_image_blob))
file_name = "temp"
fileExtension = '.jpeg'
file_name += fileExtension
image_path = "[YOUR PATH]" + file_name
im = Image.open(io.BytesIO(image))
im.save(image_path, 'jpeg')
path = image_path
return path, file_name
def analyze_picture(picture64):
return_dict = {"trafficLight": False,
"green": True,
"cars": False}
analyze_url = endpoint + "vision/v2.1/analyze"
filename = "temp.jpeg"
image_path,filename = blob_to_image_converter(picture64)
image_data = open(image_path, "rb").read()
headers = {'Ocp-Apim-Subscription-Key': subscription_key,
'Content-Type': 'application/octet-stream'}
params = {'visualFeatures': 'Objects'}
response = requests.post(analyze_url, headers=headers, params=params, data=image_data)
analysis = response.json()
for object in analysis["objects"]:
if object["object"] == "traffic light":
return_dict["green"] = crop_out_image(filename,[object][0]["rectangle"]["x"],
[object][0]["rectangle"]["w"],[object][0]["rectangle"]["y"],
[object][0]["rectangle"]["h"])
return_dict["trafficLight"] = True
if object == "stoplight":
return_dict["green"] = False
if object == "car" or object == "bus":
return_dict["cars"] = True
os.remove(filename)
return return_dict