-
Notifications
You must be signed in to change notification settings - Fork 3
/
food_recognize.py
87 lines (63 loc) · 2.31 KB
/
food_recognize.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
"""
Recognize food: fruit, vegetable
"""
import io
import os
from datetime import datetime
import cv2
from google.cloud import vision_v1p3beta1 as vision
# Setup google authen client key
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'client_key.json'
# Source path content all images
SOURCE_PATH = "E:/temp_uploads/Photos/Fruit/"
FOOD_TYPE = 'Fruit' # 'Vegetable'
def load_food_name(food_type):
"""
Load all known food type name.
:param food_type: Fruit or Vegetable
:return:
"""
names = [line.rstrip('\n').lower() for line in open( food_type + '.dict')]
return names
def recognize_food(img_path, list_foods):
start_time = datetime.now()
# Read image with opencv
img = cv2.imread(img_path)
# Get image size
height, width = img.shape[:2]
# Scale image
img = cv2.resize(img, (800, int((height * 800) / width)))
# Save the image to temp file
cv2.imwrite(SOURCE_PATH + "output.jpg", img)
# Create new img path for google vision
img_path = SOURCE_PATH + "output.jpg"
# Create google vision client
client = vision.ImageAnnotatorClient()
# Read image file
with io.open(img_path, 'rb') as image_file:
content = image_file.read()
image = vision.types.Image(content=content)
# Recognize text
response = client.label_detection(image=image)
labels = response.label_annotations
for label in labels:
# if len(text.description) == 10:
desc = label.description.lower()
score = round(label.score, 2)
print("label: ", desc, " score: ", score)
if (desc in list_foods):
# score = round(label.score, 3)
# print(desc, 'score: ', score)
# Put text license plate number to image
cv2.putText(img, desc.upper() + " ???", (300, 150), cv2.FONT_HERSHEY_SIMPLEX, 1, (50, 50, 200), 2)
cv2.imshow('Recognize & Draw', img)
cv2.waitKey(0)
# Get first fruit only
break
print('Total time: {}'.format(datetime.now() - start_time))
print('---------- Start FOOD Recognition --------')
list_foods = load_food_name(FOOD_TYPE)
print(list_foods)
path = SOURCE_PATH + '1.jpg'
recognize_food(path, list_foods)
print('---------- End ----------')