Skip to content

Commit 975ceac

Browse files
authored
3.0.1 (#42)
* Fix bug where end of detection caused crash * Cleanup * Update version * Formatting
1 parent 0a559fe commit 975ceac

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
default_target: local
22

33
COMMIT_HASH := $(shell git log -1 --pretty=format:"%h"|tail -1)
4-
VERSION = 3.0.0
4+
VERSION = 3.0.1
55

66
local:
77
cd web; flutter build web;

swatch/detection.py

+18-17
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
import datetime
44
import logging
55
import multiprocessing
6-
import random
7-
import string
86
import threading
97
from typing import Any, Dict
108

119
from swatch.config import CameraConfig, SwatchConfig
1210
from swatch.image import ImageProcessor
1311
from swatch.models import Detection
1412
from swatch.snapshot import SnapshotProcessor
13+
from swatch.util import get_random_suffix
1514

1615

1716
logger = logging.getLogger(__name__)
@@ -77,7 +76,7 @@ def __handle_detections__(self, detection_result: Dict[str, Any]) -> None:
7776
self.obj_data[non_unique_id] = {}
7877

7978
unique_id = (
80-
f"{non_unique_id}.{''.join(random.choices(string.ascii_lowercase + string.digits, k=6))}"
79+
f"{non_unique_id}.{get_random_suffix()}"
8180
if not self.obj_data[non_unique_id].get("id")
8281
else self.obj_data[non_unique_id]["id"]
8382
)
@@ -86,21 +85,22 @@ def __handle_detections__(self, detection_result: Dict[str, Any]) -> None:
8685
self.obj_data[non_unique_id]["zone_name"] = zone_name
8786
self.obj_data[non_unique_id]["variant"] = object_result["variant"]
8887

89-
top_area = max([d["area"] for d in object_result["objects"]])
90-
best_box = next(
91-
d for d in object_result["objects"] if d["area"] == top_area
92-
)["box"]
88+
if object_result.get("objects"):
89+
top_area = max([d["area"] for d in object_result["objects"]])
90+
best_box = next(
91+
d for d in object_result["objects"] if d["area"] == top_area
92+
)["box"]
9393

94-
if top_area > self.obj_data[non_unique_id].get("top_area", 0):
95-
self.obj_data[non_unique_id]["top_area"] = top_area
94+
if top_area > self.obj_data[non_unique_id].get("top_area", 0):
95+
self.obj_data[non_unique_id]["top_area"] = top_area
9696

97-
# save snapshot with best area
98-
self.snap_processor.save_detection_snapshot(
99-
cam_name,
100-
zone_name,
101-
unique_id,
102-
best_box,
103-
)
97+
# save snapshot with best area
98+
self.snap_processor.save_detection_snapshot(
99+
cam_name,
100+
zone_name,
101+
unique_id,
102+
best_box,
103+
)
104104

105105
if not self.obj_data[non_unique_id].get("id"):
106106
self.obj_data[non_unique_id]["id"] = unique_id
@@ -113,6 +113,7 @@ def __handle_detections__(self, detection_result: Dict[str, Any]) -> None:
113113
del self.obj_data[non_unique_id]
114114

115115
def run(self) -> None:
116+
# pylint: disable=singleton-comparison
116117
logger.info("Starting Auto Detection for %s", self.config.name)
117118

118119
while not self.stop_event.wait(self.config.auto_detect):
@@ -123,7 +124,7 @@ def run(self) -> None:
123124

124125
# ensure db doesn't contain bad data after shutdown
125126
Detection.update(end_time=datetime.datetime.now().timestamp()).where(
126-
Detection.end_time is None
127+
Detection.end_time == None
127128
).execute()
128129
logger.info("Stopping Auto Detection for %s", self.config.name)
129130

swatch/util.py

+13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Utilities and convenience funs."""
22

3+
import random
4+
import string
35
from typing import Any, Dict, Set, Tuple
46

57
from colorthief import ColorThief
@@ -9,6 +11,9 @@
911
from swatch.config import ColorVariantConfig, ObjectConfig
1012

1113

14+
### Image utils
15+
16+
1217
def mask_image(crop: Any, color_variant: ColorVariantConfig) -> Tuple[Any, int]:
1318
"""Mask an image with color values"""
1419
color_lower = (
@@ -65,3 +70,11 @@ def parse_colors_from_image(test_image: Any) -> tuple[str, set[str]]:
6570
main_color = color_thief.get_color(quality=1)
6671
palette = color_thief.get_palette(color_count=3)
6772
return (main_color, palette)
73+
74+
75+
### String utils
76+
77+
78+
def get_random_suffix():
79+
"""Returns 6 random character suffix string."""
80+
return "".join(random.choices(string.ascii_lowercase + string.digits, k=6))

0 commit comments

Comments
 (0)