File size: 3,680 Bytes
014de59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Aug  4 13:48:50 2024

@author: ysnrfd
"""

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Advanced Ghost Detection Script with Machine Learning Integration and Real-Time Performance Enhancements
"""

import cv2
import numpy as np
from threading import Thread
from queue import Queue
from ultralytics import YOLO

class AdvancedGhostDetector:
    def __init__(self, video_source=0, contour_area_threshold=100, model_path='yolov8s.pt'):
        self.video_source = video_source
        self.contour_area_threshold = contour_area_threshold
        self.background_subtractor = cv2.createBackgroundSubtractorMOG2()
        self.cap = cv2.VideoCapture(self.video_source)
        self.frame_queue = Queue(maxsize=10)
        self.model = YOLO(model_path)

        if not self.cap.isOpened():
            raise IOError("Error: Could not open camera.")

    def capture_frames(self):
        while True:
            ret, frame = self.cap.read()
            if not ret:
                print("Error: Failed to capture image")
                break
            if not self.frame_queue.full():
                self.frame_queue.put(frame)

    def process_frame(self, frame):
        # Apply background subtraction
        fg_mask = self.background_subtractor.apply(frame)

        # Find contours of the detected objects
        contours, _ = cv2.findContours(fg_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

        # Draw contours on the frame
        for contour in contours:
            if cv2.contourArea(contour) > self.contour_area_threshold:
                x, y, w, h = cv2.boundingRect(contour)
                cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
                cv2.putText(frame, "Anomaly Detected", (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)

        return frame

    def detect_objects(self, frame):
        # Convert frame to RGB for YOLOv8
        rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        results = self.model(rgb_frame, imgsz=320)

        # Draw bounding boxes and labels on the frame
        for result in results:
            boxes = result.boxes.data.cpu().numpy()
            for box in boxes:
                x1, y1, x2, y2, score, class_id = map(int, box)
                label = f"{self.model.names[class_id]}: {score:.2f}"
                cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
                cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)

        return frame

    def display_frame(self, frame):
        # Display the resulting frame
        cv2.imshow('Advanced Ghost Detector', frame)

    def run(self):
        capture_thread = Thread(target=self.capture_frames, daemon=True)
        capture_thread.start()

        while True:
            if not self.frame_queue.empty():
                frame = self.frame_queue.get()
                processed_frame = self.process_frame(frame)
                detected_frame = self.detect_objects(processed_frame)
                self.display_frame(detected_frame)

                # Break the loop on 'q' key press
                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break

        self.cleanup()

    def cleanup(self):
        # Release the capture and close all windows
        self.cap.release()
        cv2.destroyAllWindows()

if __name__ == "__main__":
    try:
        detector = AdvancedGhostDetector(video_source=0, contour_area_threshold=1000, model_path='yolov8n.pt')
        detector.run()
    except Exception as e:
        print(f"An error occurred: {e}")
        cv2.destroyAllWindows()