Code With Python
39K subscribers
841 photos
24 videos
22 files
746 links
This channel delivers clear, practical content for developers, covering Python, Django, Data Structures, Algorithms, and DSA – perfect for learning, coding, and mastering key programming skills.
Admin: @HusseinSheikho || @Hussein_Sheikho
Download Telegram
# Open the video file
video_path = 'industrial_video.mp4'
cap = cv2.VideoCapture(video_path)

# Loop through the video frames
while cap.isOpened():
# Read a frame from the video
success, frame = cap.read()

if success:
# Run YOLOv8 inference on the frame
results = model(frame)

# A flag to check if fire was detected in the current frame
fire_detected_in_frame = False

# Visualize the results on the frame
annotated_frame = results[0].plot()

# Process detection results
for r in results:
for box in r.boxes:
# Check if the detected class is 'fire'
# model.names[0] should correspond to 'fire' in your custom model
if model.names[int(box.cls[0])] == 'fire' and box.conf[0] > 0.5:
fire_detected_in_frame = True
break

# If fire is detected and alarm is not already on, trigger alarm
if fire_detected_in_frame and not alarm_on:
alarm_on = True
# Run the alarm sound in a background thread to not block video feed
alarm_thread = threading.Thread(target=play_alarm)
alarm_thread.start()

# Display the annotated frame
cv2.imshow("YOLOv8 Fire Detection", annotated_frame)

# Break the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord("q"):
break
else:
# Break the loop if the end of the video is reached
break

# Release the video capture object and close the display window
cap.release()
cv2.destroyAllWindows()

# Hashtags: #RealTimeDetection #VideoProcessing #OpenCV


---

#Step 4: Results and Discussion

After running the script, you will see a window playing the video. When the model detects an object it identifies as 'fire' with a confidence score above 50%, it will:
• Draw a colored box around the fire.
• Print "ALARM: Fire Detected!" to the console.
• Play the alarm.wav sound.

Discussion of Results:
Model Performance: The accuracy of this system depends entirely on the quality of your custom-trained model (fire_model.pt). A model trained on a diverse dataset of industrial fires (different lighting, angles, sizes) will perform best.
False Positives: The system might incorrectly identify orange/red lights, reflections, or welding sparks as fire. This is a common challenge. To fix this, you need to add more "negative" images (images of things that look like fire but aren't) to your training dataset.
Thresholding: The confidence threshold (box.conf[0] > 0.5) is a critical parameter. A lower value increases the chance of detecting real fires but also increases false alarms. A higher value reduces false alarms but might miss smaller or less obvious fires. You must tune this value based on your specific environment.
Real-World Implementation: For a real industrial facility, you would replace the video file with a live camera stream (cv2.VideoCapture(0) for a webcam) and integrate the alarm logic with a physical siren or a central monitoring system via an API or GPIO pins.

#ProjectComplete #AIforGood #IndustrialSafety

━━━━━━━━━━━━━━━
By: @DataScience4
1
By combining all the code from the steps above into database.py and main.py, you have a robust, database-driven desktop application.

Results:
Data Persistence: Your inventory and invoice data is saved in warehouse.db and will be there when you restart the application.
Integrated Workflow: Adding a purchase directly increases stock. A sale checks for and decreases stock. Production consumes raw materials and creates finished goods, all reflected in the central inventory table.
Separation of Concerns: The UI logic in main.py is cleanly separated from the data logic in database.py, making the code easier to maintain and extend.
Reporting: You can easily export a snapshot of your current inventory to a CSV file for analysis in other programs like Excel or Google Sheets.

Discussion and Next Steps:
Scalability: While SQLite is excellent for small-to-medium applications, a large-scale, multi-user system would benefit from a client-server database like PostgreSQL or MySQL.
Invoice Complexity: The current invoice system is simplified. A real system would allow multiple items per invoice and store historical invoice data for viewing and printing.
User Interface (UI/UX): The UI is functional but could be greatly improved with better layouts, icons, search/filter functionality in tables, and more intuitive workflows.
Error Handling: The error handling is basic. A production-grade app would have more comprehensive checks for user input and database operations.
Advanced Features: Future additions could include user authentication, supplier and customer management, barcode scanning, and more detailed financial reporting.

This project forms a powerful template for building custom internal business tools with Python.

#ProjectComplete #SoftwareEngineering #ERP #PythonGUI #BusinessApp

━━━━━━━━━━━━━━━
By: @DataScience4