|
12 | 12 | ############################################################################### |
13 | 13 |
|
14 | 14 | from pathlib import Path |
| 15 | +from typing import List |
15 | 16 | import supervision as sv |
16 | 17 | from supervision import Point, Detections |
17 | 18 | from ultralytics import YOLO |
18 | 19 | import torch |
19 | 20 | import argparse |
20 | 21 | import cv2 |
21 | 22 | from os import environ |
22 | | -from paho.mqtt.client import Client as MqttClient |
23 | | -from paho.mqtt.client import CallbackAPIVersion |
| 23 | +# from paho.mqtt.client import Client as MqttClient |
| 24 | +# from paho.mqtt.client import CallbackAPIVersion |
24 | 25 | import json |
25 | 26 |
|
| 27 | +import pi_serial |
| 28 | + |
26 | 29 | DEFAULT_MODEL_PATH = Path("./model.pt") |
27 | 30 | DEFAULT_WEBCAM_PORT = 0 |
28 | 31 | DEFAULT_CONFIDENCE_THRESHOLD = 0.5 |
29 | | -LOCAL_MQTT_BROKER_URL = environ.get("LOCAL_MQTT_BROKER_URL", None) |
30 | | -LOCAL_MQTT_BROKER_PORT = environ.get("LOCAL_MQTT_BROKER_PORT", 1883) |
31 | | -MQTT_VISION_DATA_TOPIC = 'vision/data' |
| 32 | +# LOCAL_MQTT_BROKER_URL = environ.get("LOCAL_MQTT_BROKER_URL", None) |
| 33 | +# LOCAL_MQTT_BROKER_PORT = environ.get("LOCAL_MQTT_BROKER_PORT", 1883) |
| 34 | +# MQTT_VISION_DATA_TOPIC = 'vision/data' |
32 | 35 |
|
33 | 36 | def main(): |
34 | 37 | # Parse command line arguments |
@@ -77,11 +80,11 @@ def main(): |
77 | 80 | exit(1) |
78 | 81 |
|
79 | 82 | # Create MQTT client |
80 | | - if LOCAL_MQTT_BROKER_URL is None: |
81 | | - mqtt_client = None |
82 | | - else: |
83 | | - mqtt_client = MqttClient(CallbackAPIVersion.VERSION2) |
84 | | - mqtt_client.connect(LOCAL_MQTT_BROKER_URL, LOCAL_MQTT_BROKER_PORT) |
| 83 | + # if LOCAL_MQTT_BROKER_URL is None: |
| 84 | + # mqtt_client = None |
| 85 | + # else: |
| 86 | + # mqtt_client = MqttClient(CallbackAPIVersion.VERSION2) |
| 87 | + # mqtt_client.connect(LOCAL_MQTT_BROKER_URL, LOCAL_MQTT_BROKER_PORT) |
85 | 88 |
|
86 | 89 | # For webcam access |
87 | 90 | if args.video == DEFAULT_WEBCAM_PORT or args.video.isdigit(): |
@@ -141,14 +144,14 @@ def main(): |
141 | 144 | continue |
142 | 145 |
|
143 | 146 | # Get results from model |
144 | | - results = model(frame, verbose=False)[0] |
| 147 | + results = model.predict(frame, verbose=False)[0] |
145 | 148 | if not results: |
146 | 149 | continue |
147 | 150 |
|
148 | 151 | # Pull out bounding boxes, class IDs, confidence levels, and positions |
149 | | - boxes = results.boxes.data.cpu().numpy() |
| 152 | + boxes = results.boxes.data.cuda().numpy() |
150 | 153 | class_ids = boxes[:, -1].astype(int) |
151 | | - confidences = results.boxes.conf.cpu().numpy() |
| 154 | + confidences = results.boxes.conf.cuda().numpy() |
152 | 155 | xyxy = boxes[:, :4] |
153 | 156 | # Create detections object |
154 | 157 | detections = Detections( |
@@ -206,28 +209,30 @@ def main(): |
206 | 209 | # Count objects crossing the line |
207 | 210 | out_to_in, in_to_out = line_counter.trigger(detections=tracked_detections) |
208 | 211 |
|
209 | | - if mqtt_client is not None: |
210 | | - item_change_counts = dict() |
211 | | - # Iterate through all detections to send a message for each one that passed the line |
212 | | - for i, detection in enumerate(tracked_detections): |
213 | | - # Get the label for this detection |
214 | | - label = labels[i] |
215 | | - if out_to_in[i]: |
216 | | - # Detection went from out to in |
217 | | - if label in item_change_counts: |
218 | | - # Subtract 1 from cart |
219 | | - item_change_counts[label] -= 1 |
220 | | - else: |
221 | | - item_change_counts[label] = -1 |
222 | | - if in_to_out[i]: |
223 | | - # Detection went from in to out |
224 | | - if label in item_change_counts: |
225 | | - # Add 1 to cart |
226 | | - item_change_counts[label] += 1 |
227 | | - else: |
228 | | - item_change_counts[label] = 1 |
229 | | - if len(item_change_counts) > 0: |
230 | | - mqtt_client.publish(MQTT_VISION_DATA_TOPIC, payload=json.dumps(item_change_counts)) |
| 212 | + # if mqtt_client is not None: |
| 213 | + item_change_counts = dict() |
| 214 | + # Iterate through all detections to send a message for each one that passed the line |
| 215 | + for i, detection in enumerate(tracked_detections): |
| 216 | + # Get the label for this detection |
| 217 | + label = labels[i] |
| 218 | + if out_to_in[i]: |
| 219 | + # Detection went from out to in |
| 220 | + if label in item_change_counts: |
| 221 | + # Subtract 1 from cart |
| 222 | + item_change_counts[label] -= 1 |
| 223 | + else: |
| 224 | + item_change_counts[label] = -1 |
| 225 | + if in_to_out[i]: |
| 226 | + # Detection went from in to out |
| 227 | + if label in item_change_counts: |
| 228 | + # Add 1 to cart |
| 229 | + item_change_counts[label] += 1 |
| 230 | + else: |
| 231 | + item_change_counts[label] = 1 |
| 232 | + if len(item_change_counts) > 0: |
| 233 | + data = json.dumps(item_change_counts) + '\n' |
| 234 | + pi_serial.UART.write(data.encode('utf-8')) |
| 235 | + # mqtt_client.publish(MQTT_VISION_DATA_TOPIC, payload=json.dumps(item_change_counts)) |
231 | 236 |
|
232 | 237 |
|
233 | 238 |
|
|
0 commit comments