-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal_RRR.py
141 lines (119 loc) · 4.28 KB
/
local_RRR.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
import io
import os
import tkinter as tk
from tkinter import filedialog
import sys
import cv2
from difflib import get_close_matches
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
# =======================================================
# ======================= Classes =======================
# =======================================================
# =======================================================
# ====================== Functions ======================
# =======================================================
def localize_objects(path):
"""Localize objects in the local image.
Args:
path: The path to the local file.
"""
from google.cloud import vision
client = vision.ImageAnnotatorClient()
with open(path, 'rb') as image_file:
content = image_file.read()
image = vision.Image(content=content)
objects = client.object_localization(
image=image).localized_object_annotations
print('Number of objects found: {}'.format(len(objects)))
labels = []
for object_ in objects:
print('\n{} (confidence: {})'.format(object_.name, object_.score))
labels.append(object_.name)
print('Normalized bounding polygon vertices: ')
for vertex in object_.bounding_poly.normalized_vertices:
print(' - ({}, {})'.format(vertex.x, vertex.y))
return labels
def localize_objects_uri(uri):
"""Localize objects in the image on Google Cloud Storage
Args:
uri: The path to the file in Google Cloud Storage (gs://...)
"""
from google.cloud import vision
client = vision.ImageAnnotatorClient()
image = vision.Image()
image.source.image_uri = uri
objects = client.object_localization(
image=image).localized_object_annotations
print('Number of objects found: {}'.format(len(objects)))
for object_ in objects:
print('\n{} (confidence: {})'.format(object_.name, object_.score))
print('Normalized bounding polygon vertices: ')
for vertex in object_.bounding_poly.normalized_vertices:
print(' - ({}, {})'.format(vertex.x, vertex.y))
# =======================================================
# ======================== Start ========================
# =======================================================
# Read dataset into dictionary
compost_dict = {}
key_list = []
with open("compost_dataset.txt") as f:
for line in f:
key_flag = True
buffer_key = ""
buffer_val = ""
for char in line:
if key_flag:
if char != ":":
buffer_key += char
else:
key_flag = False
else:
buffer_val += char
new_key = buffer_key[:-1]
new_val = buffer_val[1:].replace('\n', '')
compost_dict[new_key] = new_val
key_list.append(new_key)
print("Dataset Length: {}".format(len(compost_dict)))
key_list_len = len(key_list)
print("Keys: {}\n".format(key_list_len))
# Get user image
print("Select File")
root = tk.Tk()
root.withdraw()
filename = filedialog.askopenfilename()
if filename == "":
print("--- Cancelled ---")
sys.exit(0)
print("Loaded from {} successfully".format(filename))
# Perform object recognition
labels = localize_objects(filename)
print()
print("=== === === Results === === ===")
# Check if each object is compostable
for obj in labels:
# get close objects in our keys if there
str_matches = get_close_matches(obj, key_list, key_list_len, 0.53)
if str_matches:
# if any of the matches are a yes or maybe
yes_flag = False
maybe_flag = False
for str_match in str_matches:
compostable_val = compost_dict.get(str_match, None)
if compostable_val == "yes":
yes_flag = True
break
elif compostable_val == "maybe":
maybe_flag = True
if yes_flag:
print("{}: YES, compost-able".format(obj))
elif maybe_flag:
print("{}: MAYBE, careful of bacteria or amount".format(obj))
else:
print("{}: NO, please no! do NOTTT compost this".format(obj))
else:
print("{}: NO, please no! do NOTTT compost this".format(obj))
# Display image for reference
img = mpimg.imread(filename)
plt.imshow(img)
plt.show()