-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect.py
executable file
·159 lines (133 loc) · 5.02 KB
/
detect.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
157
158
159
#!/usr/bin python3
# python3
#
# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Example using TF Lite to detect objects with the Raspberry Pi camera."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse
import io
import re
import time
import datetime
import requests
import numpy as np
from PIL import Image
from tflite_runtime.interpreter import Interpreter
import os
from config import config_dict
CAMERA_WIDTH = 640
CAMERA_HEIGHT = 480
#CHANNELID = config_dict['CHANNELID']
KEY = config_dict['KEY']
def load_labels(path):
"""Loads the labels file. Supports files with or without index numbers."""
with open(path, 'r', encoding='utf-8') as f:
lines = f.readlines()
labels = {}
for row_number, content in enumerate(lines):
pair = re.split(r'[:\s]+', content.strip(), maxsplit=1)
if len(pair) == 2 and pair[0].strip().isdigit():
labels[int(pair[0])] = pair[1].strip()
else:
labels[row_number] = pair[0].strip()
return labels
def set_input_tensor(interpreter, image):
"""Sets the input tensor."""
tensor_index = interpreter.get_input_details()[0]['index']
input_tensor = interpreter.tensor(tensor_index)()[0]
input_tensor[:, :] = image
def get_output_tensor(interpreter, index):
"""Returns the output tensor at the given index."""
output_details = interpreter.get_output_details()[index]
tensor = np.squeeze(interpreter.get_tensor(output_details['index']))
return tensor
def detect_objects(interpreter, image, threshold):
"""Returns a list of detection results, each a dictionary of object info."""
set_input_tensor(interpreter, image)
interpreter.invoke()
# Get all output details
boxes = get_output_tensor(interpreter, 0)
classes = get_output_tensor(interpreter, 1)
scores = get_output_tensor(interpreter, 2)
count = int(get_output_tensor(interpreter, 3))
results = []
for i in range(count):
if scores[i] >= threshold:
result = {
'bounding_box': boxes[i],
'class_id': classes[i],
'score': scores[i]
}
results.append(result)
return results
lbl = []
def annotate_objects(results, labels):
"""Returns labels and scores for an inference"""
for obj in results:
lbl.append([labels[obj['class_id']], obj['score']])
return lbl
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
'--model', help='File path of .tflite file.', required=False, default='/home/pi/Public/trail-counter-RPi3-setup/detect.tflite')
parser.add_argument(
'--labels', help='File path of labels file.', required=False, default='/home/pi/Public/trail-counter-RPi3-setup/coco_labels.txt')
parser.add_argument(
'--threshold',
help='Score threshold for detected objects.',
required=False,
type=float,
default=0.55)
parser.add_argument(
'--image',
help='Path to image. Eg. /path/to/image/imageName.jpg',
required=False, default='/home/pi/Public/images/counter_image.jpg')
args = parser.parse_args()
labels = load_labels(args.labels)
interpreter = Interpreter(args.model)
interpreter.allocate_tensors()
_, input_height, input_width, _ = interpreter.get_input_details()[0]['shape']
with Image.open(args.image).convert('RGB').resize((input_width, input_height), Image.ANTIALIAS) as f:
results = detect_objects(interpreter, f, args.threshold)
detect = annotate_objects(results, labels)
bicycle = 0
person = 0
horse = 0
car = 0
for d in detect:
if d[0] == 'bicycle':
bicycle += 1
if d[0] == 'person':
person += 1
if d[0] == 'horse':
horse += 1
if d[0] == 'car':
car += 1
main_url = 'https://api.thingspeak.com/update?api_key=' + KEY
date = str(datetime.datetime.now())
payload = dict(field1=date, field3=bicycle, field4=person, field5=horse, field6=car)
for attempt in range (10):
try:
r = requests.post(main_url, params=payload)
print(r.url)
except requests.exceptions.ConnectionError:
pass
else:
break
if __name__ == '__main__':
main()