0

Hi I've been working an opencv and python project which the raspi should detect and recognize a face and detect a motion. It works fine with my laptop but after I run it on my raspi the raspi crashes. There's no error or whatsoever but I don't know what causes this.

Hardware: Raspberry pi 3B and camera module v2

OS: Raspbian Stretch Full

Software: Python 3.6 and OpenCV 4.0

This is my full code for main.py

import cv2
import numpy as np
import os
import datetime
import time
from matplotlib import pyplot as plt
from IPython.display import clear_output
from face_detector import FaceDetector
from video_camera import VideoCamera

def cut_faces(image, faces_coord):
faces = []
for (x, y, w, h) in faces_coord:
    w_rm = int(0.2 * w / 2)  # only 70, 80% of the wdithh
    faces.append(image[y: y + h, x + w_rm: x + w - w_rm])

return faces

def normalize_intensity(images):
images_norm = []
for image in images:
    is_color = len(image.shape) == 3
    if is_color:
        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    images_norm.append(cv2.equalizeHist(image))
return images_norm

def resize(images, size=(50, 50)):
images_norm = []
for image in images:
    if image.shape < size:
        image_norm = cv2.resize(image, size, interpolation=cv2.INTER_AREA)
    else:
        image_norm = cv2.resize(image, size, interpolation=cv2.INTER_CUBIC)

    images_norm.append(image_norm)

return images_norm

def normalize_faces(frame, faces_coord):
faces = cut_faces(frame, faces_coord)
faces = normalize_intensity(faces)
faces = resize(faces)
return faces

def draw_rectangle(image, coords):
for(x, y, w, h) in coords:
    w_rm = int(0.2 * w / 2)
    cv2.rectangle(image, (x + w_rm, y),
                  (x + w - w_rm, y + h), (150, 150, 0), 8)

def collect_dataset():
images = []
labels = []
labels_dic = {}
people = [person for person in os.listdir(
    "C:/Users/JeffRolan11/jeff_python/people/")]
for i, person in enumerate(people):
    labels_dic[i] = person
    for image in os.listdir("C:/Users/JeffRolan11/jeff_python/people/" + person):
        images.append(cv2.imread(
            "C:/Users/JeffRolan11/jeff_python/people/" + person + '/' + image, 0))
        labels.append(i)
return (images, np.array(labels), labels_dic)

images, labels, labels_dic = collect_dataset()

rec_lbph = cv2.face.LBPHFaceRecognizer_create()
rec_lbph.train(images, labels)

print("Model Trained Successfully")

webcam = VideoCamera()
detector = FaceDetector(
    "C:/Users/JeffRolan11/jeff_python/xml/frontal_face.xml")

cv2.namedWindow("Face Recognition Prototype", cv2.WINDOW_AUTOSIZE)

#===========Motion Detection========
fgbg = cv2.createBackgroundSubtractorMOG2(300, 400, True)
frameCount = 0
capture = cv2.VideoCapture(0)
started = time.time()
capture_duration = 10
date = datetime.datetime.now().strftime("%m-%d-%Y,%I-%M-%S-%p")
#====================================

while True:
    pred = ""
    mot = ""
    frame = webcam.get_frame()
    faces_coord = detector.detect(frame, True)
    instant = time.time()
#=========Motion Detection ===========
frameCount += 1
resizedFrame = cv2.resize(frame, (0, 0), fx=0.50, fy=0.50)
fgmask = fgbg.apply(resizedFrame)
count = np.count_nonzero(fgmask)

if (frameCount > 1 and count > 2000):
    mot = "detected"
    cv2.putText(frame, 'Motion Detected', (10, 50),
                cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)

if mot == "detected":
    print("instant")
    record_video(started, capture_duration, date, capture)
    date = datetime.datetime.now().strftime("%m-%d-%Y,%I-%M-%S-%p")
    capture = cv2.VideoCapture(0)
    started = time.time()
    webcam = VideoCamera()

cv2.imshow('Motion Contour', fgmask)
#======================================

#=========Face Recognition=============
    cv2.putText(frame, datetime.datetime.now().strftime("%A, %B %d %Y %I:%M:%S %p"), (5, frame.shape[0] - 5),
            cv2.FONT_HERSHEY_PLAIN, 1.3, (66, 53, 243), 2, cv2.LINE_AA)
if len(faces_coord):
    faces = normalize_faces(frame, faces_coord)
    for i, face in enumerate(faces):
        prediction, confidence = rec_lbph.predict(face)
        threshold = 155
        clear_output(wait=True)
        if confidence < threshold:
            pred = labels_dic[prediction].capitalize()
            cv2.putText(frame, labels_dic[prediction].capitalize(),
                        (faces_coord[i][0], faces_coord[i][1] - 10),
                        cv2.FONT_HERSHEY_PLAIN, 3, (66, 53, 243), 2)
        elif(confidence > threshold):
            pred = "Unknown"
            cv2.putText(frame, "Unknown",
                        (faces_coord[i][0], faces_coord[i][1]),
                        cv2.FONT_HERSHEY_PLAIN, 3, (66, 53, 243), 2)
        else:
            pred = ""
        draw_rectangle(frame, faces_coord)
#=======================================
cv2.imshow("Face Recognition Prototype", frame)
if cv2.waitKey(40) & 0xFF == 27:
    cv2.destroyAllWindows()
    break
webcam.video.release()

this is my video_camera.py

import cv2
import numpy as np
import os
from matplotlib import pyplot as plt
from IPython.display import clear_output

class VideoCamera(object):
    def __init__(self, index=0):
        self.video = cv2.VideoCapture(0)
        self.index = index
        print(self.video.isOpened())

    def __del__(self):
        self.video.release()

    def get_frame(self, in_grayscale=False):
        _, frame = self.video.read()
        if in_grayscale:
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        return frame

and this is my face_detector.py

import cv2
import numpy as np
import os
from matplotlib import pyplot as plt
from IPython.display import clear_output

class FaceDetector(object):
    def __init__(self, xml_path):
        self.classifier = cv2.CascadeClassifier(xml_path)

    def detect(self, image, biggest_only):
        scale_factor = 1.2
        min_neighbours = 5
        min_size = (30, 30)
        biggest_only = True
        flags = cv2.CASCADE_FIND_BIGGEST_OBJECT | \
            cv2.CASCADE_DO_ROUGH_SEARCH if biggest_only else \
            cv2.CASCADE_SCALE_IMAGE

        faces_coord = self.classifier.detectMultiScale(image,

                                                   scaleFactor=scale_factor,
                                                   minNeighbors=min_neighbours,
                                                   minSize=min_size,
                                                   flags=flags)
    return faces_coord
New contributor
JeffNUCode is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

JeffNUCode is a new contributor. Be nice, and check out our Code of Conduct.

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.