-1

I always get an error when making face recognition for web pages with video streams, opencv and flask

The error is like this:

IndentationError: unexpected indent

This is for my project, running python3, opencv,flask and raspberry pi, i have tried in raspberry pi as a server and always error, thanks

import cv2
import urllib
import pdb
import numpy as np
import face_recognition
import argparse
import imutils
import pickle
import time


face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')


class VideoCamera(object):
    def __init__(self):
        # Using OpenCV to capture from device 0. If you have trouble capturing
        # from a webcam, comment the line below out and use a video file
        # instead.
        self.video = cv2.VideoCapture(0)
        # If you decide to use video.mp4, you must have this file in the folder

        # as the main.py.
        # self.video = cv2.VideoCapture('video.mp4')


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


    def get_frame(self):
        while True:
            # pdb.set_trace()
            success, img = self.video.read(1024)
            gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            faces = face_cascade.detectMultiScale(gray, 1.3, 5)
            for (x,y,w,h) in faces:
                cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
                roi_gray = gray[y:y+h, x:x+w]
                roi_color = img[y:y+h, x:x+w]
            ret, jpeg = cv2.imencode('.jpg', img)
            return jpeg.tobytes()

    encodings = face_recognition.face_encodings(rgb, boxes)
    names = []

    # loop di semua wajah yang terdeteksi
    for encoding in encodings:
        matches = face_recognition.compare_faces(data["encodings"],
        encoding)
        name = "Unknown"

        # check apakah ada wajah yang di kenali
        if True in matches:
            matchedIdxs = [i for (i, b) in enumerate(matches) if b]
            counts = {}
            for i in matchedIdxs:
                name = data["names"][i]
                counts[name] = counts.get(name, 0) + 1
            name = max(counts, key=counts.get)
        names.append(name)

    # loop di semua wajah yang sudah di kenali
    for ((top, right, bottom, left), name) in zip(boxes, names):
        # tampilkan nama di wajah yang di kenali
        cv2.rectangle(img, (left, top), (right, bottom),
        (0, 255, 0), 2)
        y = top - 15 if top - 15 > 15 else top + 15
        cv2.putText(img, name, (left, y), cv2.FONT_HERSHEY_SIMPLEX,
        0.75, (0, 255, 0), 2)

i expect the frame for video streaming can show label name and face recognition, thanks :)

  • are some of your indentations with tab and others with spaces? – Chris Doyle Sep 13 '19 at 13:47
  • yes and then i got this error "Traceback (most recent call last): File "main.py", line 2, in from camera import VideoCamera File "/home/pi/project/web44/camera.py", line 45, in encodings = face_recognition.face_encodings(rgb, boxes) NameError: name 'rgb' is not defined" – Ihsan Magazine Sep 13 '19 at 14:03
  • yes this line `encodings = face_recognition.face_encodings(rgb, boxes)` you are passing a variable called `rgb` as the first parameter but that variable is not defined anywhere in your code. Thats why you get a name error. as your using a varaible name thats not defined. – Chris Doyle Sep 13 '19 at 14:08
  • oke thanks i will try to remove rgb – Ihsan Magazine Sep 14 '19 at 09:51

1 Answers1

0

Use pep8 library:

$ pip install pep8
$ pip install --upgrade pep8

Or the Debian package

$ pep8 --first my_python_file.py
Kshitij Saxena
  • 930
  • 8
  • 19