FIX

mail@pastecode.io avatar
unknown
python
a month ago
1.9 kB
1
Indexable
Never
import cv2
import numpy as np
import threading

def detect_camera(frame):
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(gray, (15, 15), 0)
    
    circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT, 1, 50, 
                               param1=200, 
                               param2=40,  
                               minRadius=10,  
                               maxRadius=50)  

    if circles is not None:
        return "Camera detected!"  # Return message for camera detection
    
    if np.mean(gray) < 10:  # If the frame is mostly black
        return "Black screen detected!"  # Return message for black screen detection
    
    return None

def check_camera():
    global detection_message
    while True:
        ret, frame = cap.read()
        if not ret:
            detection_message = "Unable to access the camera or no frame captured."
            break

        message = detect_camera(frame)
        if message:
            print(message)
            detection_message = message
            break
        else:
            print("No camera detected.")
            
        cv2.waitKey(100) 

image_path = 'picart 1.png'
frame = cv2.imread(image_path)

cap = cv2.VideoCapture(0)

# Check if the camera opened successfully
if not cap.isOpened():
    print("Error: Unable to access the camera. Please check permissions.")
else:
    detection_message = None
    thread = threading.Thread(target=check_camera)
    thread.start()

    cv2.imshow('Frame', frame)

    while cv2.getWindowProperty('Frame', cv2.WND_PROP_VISIBLE) >= 1:
        if detection_message:
            # Close the window when a camera or black screen is detected
            cv2.destroyWindow('Frame')
            break
        cv2.waitKey(100)

    cap.release()
    cv2.destroyAllWindows()
Leave a Comment