-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect.py
167 lines (130 loc) · 5.14 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
160
161
162
163
164
165
166
167
# Copyright 2021 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
#
# http://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.
# Modifications by Surya Vasudev
"""Main script to run the object detection routine."""
import argparse
import sys
import cv2
from tflite_support.task import core
from tflite_support.task import processor
from tflite_support.task import vision
import utils
#App window
from tkinter import *
from PIL import Image, ImageTk
import webbrowser
def querycheck():
if detection_result.detections[0].categories[0].category_name == 'person':
webbrowser.open(f'https://duckduckgo.com/?q={detection_result.detections[0].categories[0].category_name}')
else:
webbrowser.open(f'https://shopping.google.com/search?tbm=shop&hl=en-US&psb=1&ved=2ahUKEwiQ7pDCnNz8AhXcC4gJHc6LB0cQu-kFegQIABAK&q={detection_result.detections[0].categories[0].category_name}')
# Create an instance of TKinter Window or frame
win = Tk()
# Set the size of the window
win.geometry("1920x720")
# Create a Label to capture the Video frames
label =Label(win)
label.grid(row=0, column=0)
#button
btn = Button(win, text = 'Search Detected', bd = '5',command = lambda: querycheck())
btn.grid(row=10, column = 10)
def run(model: str, camera_id: int, width: int, height: int, num_threads: int,
enable_edgetpu: bool) -> None:
"""Continuously run inference on images acquired from the camera.
Args:
model: Name of the TFLite object detection model.
camera_id: The camera id to be passed to OpenCV.
width: The width of the frame captured from the camera.
height: The height of the frame captured from the camera.
num_threads: The number of CPU threads to run the model.
enable_edgetpu: True/False whether the model is a EdgeTPU model.
"""
# Start capturing video input from the camera
cap = cv2.VideoCapture(camera_id)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
# Initialize the object detection model
base_options = core.BaseOptions(
file_name=model, use_coral=enable_edgetpu, num_threads=num_threads)
detection_options = processor.DetectionOptions(
max_results=3, score_threshold=0.3)
options = vision.ObjectDetectorOptions(
base_options=base_options, detection_options=detection_options)
detector = vision.ObjectDetector.create_from_options(options)
#Taken out of the while loop to make sure that global imagecv is not called each time
global imagecv
success, imagecv = cap.read()
if not success:
sys.exit(
'ERROR: Unable to read from webcam. Please verify your webcam settings.'
)
# Continuously capture images from the camera and run inference
while cap.isOpened():
success, imagecv = cap.read()
imagecv = cv2.flip(imagecv, 1)
# Convert the imagecv from BGR to RGB as required by the TFLite model.
rgb_image = cv2.cvtColor(imagecv, cv2.COLOR_BGR2RGB)
# Create a TensorImage object from the RGB imagecv.
input_tensor = vision.TensorImage.create_from_array(rgb_image)
# Run object detection estimation using the model.
global detection_result
detection_result = detector.detect(input_tensor)
# Draw keypoints and edges on input imagecv
imagecv = utils.visualize(imagecv, detection_result)
#Fix output image colorspace for tkinter
rgb_image = cv2.cvtColor(imagecv, cv2.COLOR_BGR2RGB)
#Update tkinter window
imgtk = ImageTk.PhotoImage(image=Image.fromarray(rgb_image))
label.configure(image=imgtk)
win.update_idletasks()
win.update()
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
'--model',
help='Path of the object detection model.',
required=False,
default='efficientdet_lite0.tflite')
parser.add_argument(
'--cameraId', help='Id of camera.', required=False, type=int, default=0)
parser.add_argument(
'--frameWidth',
help='Width of frame to capture from camera.',
required=False,
type=int,
default=640)
parser.add_argument(
'--frameHeight',
help='Height of frame to capture from camera.',
required=False,
type=int,
default=480)
parser.add_argument(
'--numThreads',
help='Number of CPU threads to run the model.',
required=False,
type=int,
default=4)
parser.add_argument(
'--enableEdgeTPU',
help='Whether to run the model on EdgeTPU.',
action='store_true',
required=False,
default=False)
args = parser.parse_args()
run(args.model, int(args.cameraId), args.frameWidth, args.frameHeight,
int(args.numThreads), bool(args.enableEdgeTPU))
if __name__ == '__main__':
main()