Skip to content

Commit

Permalink
Refactor the structure
Browse files Browse the repository at this point in the history
  • Loading branch information
yihong1120 committed Aug 1, 2024
1 parent 60eab7e commit a29c2f7
Showing 1 changed file with 31 additions and 29 deletions.
60 changes: 31 additions & 29 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,17 @@ def main(
# Detect hazards in the frame
datas, _ = live_stream_detector.generate_detections(frame)

# Draw the detections on the frame
frame_with_detections, controlled_zone_polygon = (
drawing_manager.draw_detections_on_frame(frame, datas)
# Check for warnings and send notifications if necessary
warnings, controlled_zone_polygon = danger_detector.detect_danger(
datas,
)

# Convert the frame to a byte array
_, buffer = cv2.imencode('.png', frame_with_detections)
frame_bytes = buffer.tobytes()
# Draw the detections on the frame
frame_with_detections = (
drawing_manager.draw_detections_on_frame(
frame, controlled_zone_polygon, datas,
)
)

# Save the frame with detections
# save_file_name = f'{label}_{image_name}_{detection_time}'
Expand All @@ -128,6 +131,10 @@ def main(
# save_file_name
# )

# Convert the frame to a byte array
_, buffer = cv2.imencode('.png', frame_with_detections)
frame_bytes = buffer.tobytes()

# Log the detection results
logger.info(f"{label} - {image_name}")
logger.info(f"Detection time: {detection_time}")
Expand All @@ -139,34 +146,29 @@ def main(
# >5 mins since last notification
(timestamp - last_notification_time) > 300 and
# Between 7 AM and 6 PM
(7 <= current_hour < 18)
(7 <= current_hour < 18) and
# Check if there are warning messages
warnings
):
# Check for warnings and send notifications if necessary
warnings = danger_detector.detect_danger(
datas, controlled_zone_polygon,
# Combine all warnings into one message
message = f"{image_name}\n[{detection_time}]\n" + '\n'.join(
[f"{warning}" for warning in warnings],
)

# Check if there are any warnings
if warnings:
# Combine all warnings into one message
message = f"{image_name}\n[{detection_time}]\n" + '\n'.join(
[f"{warning}" for warning in warnings],
# Send notification, with image if image_name set
notification_status = line_notifier.send_notification(
message,
image=frame_bytes if frame_bytes is not None else None,
)
if notification_status == 200:
logger.warning(
f"Notification sent successfully: {message}",
)
else:
logger.error(f"Failed to send notification: {message}")

# Send notification, with image if image_name set
notification_status = line_notifier.send_notification(
message,
image=frame_bytes if frame_bytes is not None else None,
)
if notification_status == 200:
logger.warning(
f"Notification sent successfully: {message}",
)
else:
logger.error(f"Failed to send notification: {message}")

# Update the last_notification_time to the current time
last_notification_time = int(timestamp)
# Update the last_notification_time to the current time
last_notification_time = int(timestamp)

else:
logger.info(
Expand Down

0 comments on commit a29c2f7

Please sign in to comment.