Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1251

Python • Camera Window won't jump out

$
0
0
Hi there, currently I am working a drowsiness detector. Which using Raspberry Pi 4 model B, alarm buzzer and Picamera.

However when I run the code, it will stuck at this situation:
[0:34:42.880149413] [4369] INFO Camera camera_manager.cpp:284 libcamera v0.1.0+118-563cd78e
[0:34:42.911902391] [4378] WARN RPiSdn sdn.cpp:39 Using legacy SDN tuning - please consider moving SDN inside rpi.denoise
[0:34:42.914452271] [4378] INFO RPI vc4.cpp:444 Registered camera /base/soc/i2c0mux/i2c@1/ov5647@36 to Unicam device /dev/media4 and ISP device /dev/media1
[0:34:42.914529992] [4378] INFO RPI pipeline_base.cpp:1142 Using configuration file '/usr/share/libcamera/pipeline/rpi/vc4/rpi_apps.yaml'
[0:34:42.916906264] [4369] INFO Camera camera_manager.cpp:284 libcamera v0.1.0+118-563cd78e
[0:34:42.946367655] [4381] WARN RPiSdn sdn.cpp:39 Using legacy SDN tuning - please consider moving SDN inside rpi.denoise
[0:34:42.948458818] [4381] INFO RPI vc4.cpp:444 Registered camera /base/soc/i2c0mux/i2c@1/ov5647@36 to Unicam device /dev/media4 and ISP device /dev/media1
[0:34:42.948532891] [4381] INFO RPI pipeline_base.cpp:1142 Using configuration file '/usr/share/libcamera/pipeline/rpi/vc4/rpi_apps.yaml'
[0:34:42.954676789] [4369] INFO Camera camera.cpp:1183 configuring streams: (0) 640x480-XRGB8888 (1) 640x480-SGBRG10_CSI2P
[0:34:42.955414151] [4381] INFO RPI vc4.cpp:608 Sensor: /base/soc/i2c0mux/i2c@1/ov5647@36 - Selected sensor format: 640x480-SGBRG10_1X10 - Selected unicam format: 640x480-pGAA

Can someone guide and provide me some solution? I am totally a fresh beginner, and this is for my university final year project. :cry:

Below is the code:

Code:

from imutils.video import VideoStreamfrom imutils import face_utilsfrom threading import Threadimport numpy as npimport RPi.GPIO as GPIOimport argparseimport imutilsimport timeimport dlibimport cv2from picamera2 import Picamera2picam2 = Picamera2()picam2.configure(picam2.create_preview_configuration(main={"format": 'XRGB8888', "size": (640, 480)}))picam2.start()GPIO.setmode(GPIO.BOARD)GPIO.setwarnings(False)buzzer_pin = 16hog_face_detector = dlib.get_frontal_face_detector()predictor = dlib.shape_predictor("/home/cheeyong/Downloads/drowsinessDetector-master/shape_predictor_68_face_landmarks.dat")sleep = 0drowsy = 0active = 0status = ""def compute(ptA, ptB):    dist = np.linalg.norm(ptA - ptB)    return distdef blinked(a, b, c, d, e, f):    up = compute(b, d) + compute(c, e)    down = compute(a, f)    ratio = up / (2.0 * down)    if ratio > 0.25:        return 2    elif 0.21 < ratio <= 0.25:        return 1    else:        return 0while True:    im = picam2.capture_array()    gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)    faces = hog_face_detector(gray)    for face in faces:        x1, y1, x2, y2 = face.left(), face.top(), face.right(), face.bottom()        face_frame = im.copy()        cv2.rectangle(face_frame, (x1, y1), (x2, y2), (0, 255, 0), 2)        landmarks = predictor(gray, face)        landmarks = face_utils.shape_to_np(landmarks)        left_blink = blinked(landmarks[36], landmarks[37],                             landmarks[38], landmarks[41], landmarks[40], landmarks[39])        right_blink = blinked(landmarks[42], landmarks[43],                              landmarks[44], landmarks[47], landmarks[46], landmarks[45])        if left_blink == 0 or right_blink == 0:            sleep += 1            drowsy = 0            active = 0            if sleep > 1:                status = "SLEEPING !!!"                print("SLEEPING !!!")                GPIO.output(buzzer_pin, GPIO.HIGH)        elif 0.21 < left_blink == 1 or right_blink == 1:            sleep = 0            active = 0            drowsy += 1            if drowsy > 1:                status = "Drowsy !"                GPIO.output(buzzer_pin, GPIO.HIGH)        else:            drowsy = 0            sleep = 0            active += 1            if active > 1:                status = "Active :)"                print("Active !!!")                GPIO.output(buzzer_pin, GPIO.LOW)        cv2.putText(im, status, (100, 100), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 0, 255), 3)        for n in range(0, 68):            x, y = landmarks[n]            cv2.circle(face_frame, (x, y), 1, (255, 255, 255), -1)    cv2.imshow("Frame", im)    key = cv2.waitKey(1)    if key == 27:        breakcamera.release()cv2.destroyAllWindows()

Statistics: Posted by matteo2002starter — Thu Feb 01, 2024 2:46 pm



Viewing all articles
Browse latest Browse all 1251

Trending Articles