# --- Loitering Logic --- (This code continues inside the while loop)
for box, track_id in zip(boxes, track_ids):
x, y, w, h = box
center_point = (int(x), int(y))
# Check if the center of the person is inside the alert zone
is_inside_zone = cv2.pointPolygonTest(ALERT_ZONE_POLYGON, center_point, False) >= 0
if is_inside_zone:
# If person is inside, start or check their timer
if track_id not in loitering_timers:
# First time this person is detected in the zone
loitering_timers[track_id] = time.time()
else:
# Person is still in the zone, check duration
elapsed_time = time.time() - loitering_timers[track_id]
if elapsed_time > LOITERING_THRESHOLD_SECONDS:
# Loitering detected! Trigger alert.
alert_triggered_ids.add(track_id)
else:
# If person leaves the zone, reset their timer
if track_id in loitering_timers:
del loitering_timers[track_id]
if track_id in alert_triggered_ids:
alert_triggered_ids.remove(track_id) # Optional: reset alert when they leave
# Add visual alert text for tracked individuals who triggered the alert
for track_id in alert_triggered_ids:
# This part requires re-iterating through boxes to find the right one to draw on.
# A more optimized way would be to store box coordinates with the track_id.
for box, tid in zip(results[0].boxes.xyxy.cpu(), track_ids):
if tid == track_id:
x1, y1, _, _ = map(int, box)
cv2.putText(annotated_frame, "ALERT: LOITERING!", (x1, y1 - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
break
# Display the annotated frame
cv2.imshow("Suspicious Activity Tracker", annotated_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
# Hashtags: #SecurityAI #AlertSystem #HomeSafety
---
#Step 5: Results and DiscussionWhen you run the script, you will see your video with:
• A yellow polygon outlining your defined security zone.
• Bounding boxes around each person with a unique tracking ID.
• If a person stays inside the zone for longer than
LOITERING_THRESHOLD_SECONDS, a red "ALERT: LOITERING!" message will appear above their bounding box.Discussion and Ethical Considerations:
Defining "Suspicious": This system does not understand "suspicious behavior." It only follows a simple rule:
person + location + time. The interpretation of this as "suspicious" is a human one.False Positives: The system will trigger an alert for anyone who meets the criteria, including a homeowner, a delivery driver waiting for a signature, or a neighbor stopping to chat. The
LOITERING_THRESHOLD_SECONDS must be carefully tuned to balance security with convenience.Tracking Stability: In low-light conditions, the tracker might lose a person and assign them a new ID when they reappear, resetting the timer. Using a higher-quality camera or a YOLO model fine-tuned on night-time data (e.g., from thermal or infrared cameras) would significantly improve performance.
Bias and Fairness: AI models can be less accurate in identifying individuals from underrepresented groups in their training data. In a security context, this could lead to a higher rate of missed detections or false alarms for certain people, which is a serious ethical issue. This tool should be used to assist human judgment, not replace it.
#ProjectComplete #AIforGood #ResponsibleAI
━━━━━━━━━━━━━━━
By: @DataScienceN ✨
Discussion and Potential Improvements:
Real Barcode Scanner: This application works directly with a USB barcode scanner. A scanner acts as a keyboard, so when it scans a code, it types the numbers and sends an "Enter" keystroke, which perfectly triggers our
Data Integrity: We added a basic check for stock (
Features for a Real Pharmacy: A production-level system would need many more features: prescription management, patient records, batch tracking for recalls, advanced reporting (e.g., top-selling drugs, low-stock alerts), user accounts with different permission levels, and receipt printing.
Database: SQLite is perfect for a single-user, standalone application. For a pharmacy with multiple terminals, a client-server database like PostgreSQL or MySQL would be necessary.
This project provides a solid foundation, demonstrating how to integrate hardware (like a barcode scanner) with a database-backed desktop application to solve a real-world business problem.
#ProjectComplete #SoftwareEngineering #PythonGUI #HealthTech
━━━━━━━━━━━━━━━
By: @DataScienceN ✨
Real Barcode Scanner: This application works directly with a USB barcode scanner. A scanner acts as a keyboard, so when it scans a code, it types the numbers and sends an "Enter" keystroke, which perfectly triggers our
returnPressed signal.Data Integrity: We added a basic check for stock (
quantity > 0). A more robust system would check if the quantity in the cart exceeds the quantity in stock before allowing the sale to complete.Features for a Real Pharmacy: A production-level system would need many more features: prescription management, patient records, batch tracking for recalls, advanced reporting (e.g., top-selling drugs, low-stock alerts), user accounts with different permission levels, and receipt printing.
Database: SQLite is perfect for a single-user, standalone application. For a pharmacy with multiple terminals, a client-server database like PostgreSQL or MySQL would be necessary.
This project provides a solid foundation, demonstrating how to integrate hardware (like a barcode scanner) with a database-backed desktop application to solve a real-world business problem.
#ProjectComplete #SoftwareEngineering #PythonGUI #HealthTech
━━━━━━━━━━━━━━━
By: @DataScienceN ✨
❤2