In this script below I am experimenting around with OpenCV and calculating a distance to my laptop webcam of a face detection with Haar Cascades. I am using a windows 10 laptop with a web cam, Python 3.6, and OpenCV 3.4.
I am having an issue with the OpenCV.putext of displaying this calculated value on the view of the video stream…
text = "Inches{}".format(np.int(inches))
cv2.putText(gray, text, (roi[0] - 10, roi[1] - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
The code will run if this is commented out.. Any tips for what I am doing wrong would be greatly appreciated!
import numpy as np
import imutils
import cv2
from imutils.video import VideoStream
from imutils.video import FPS
import time
def distance_to_camera(knownWidth, focalLength, perWidth):
# compute and return the distance from the maker to the camera
return (knownWidth * focalLength) / perWidth
face_cascade = cv2.CascadeClassifier('C:/Users/Haar/frontalFace10/haarcascade_frontalface_alt.xml')
#Calculated from a different script
focalLength = 709.0909090909091
#average human head width
knownWidth = 7
# Initialize mutithreading the video stream.
camera = VideoStream(src=0).start()
# Allow the camera to warm up.
time.sleep(2.0)
#start FPS
fps = FPS().start()
roi = None
while True:
image = camera.read()
image = imutils.resize(image, width=500)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5);
for (x, y, w, h) in faces:
cv2.rectangle(gray,(x,y),(x+w,y+h),(255,255,255),2)
roi = gray[y:y+h, x:x+w]
if roi is None:
pass
else:
inches = distance_to_camera(knownWidth, focalLength, roi.shape[1])
print(inches)
text = "Inches{:.2f}".format(np.int(inches))
cv2.putText(gray, text, (roi[0] - 10, roi[1] - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
cv2.imshow("gray", gray)
key = cv2.waitKey(1) & 0xFF
fps.update()
# if the `q` key was pressed, break from the loop
if key == ord("q"):
break
fps.stop()
print("[INFO] elapsed time: {:.2f}".format(fps.elapsed()))
print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
camera.stop()
cv2.destroyAllWindows()
This is the full traceback of the error:
Traceback (most recent call last):
File "C:\Users\distance-to-camera\selectHaar3.py", line 53, in <module>
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
TypeError: only size-1 arrays can be converted to Python scalars
>>>
roi[0] and roi[1] are arrays.
I think wat you are trying to do is:
cv2.putText(gray, text, (x - 10, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
Related
I am making a project to recognize the person by his/her face using OpenCV library in Python.
My code is :-
# Import OpenCV and sys libraries
import cv2
import sys
import os
import numpy as np
subjects = ["", "Daksh", "Ishika", "Dashya", "Shivang"]
#Create the haar cascade
faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
def draw_rectangle(img, rect):
(x, y, w, h) = rect
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
def draw_text(img, text, x, y):
cv2.putText(img, text, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 255, 0), 2)
def imgdetect(gray,image):
#Detect faces in the image
faces = faceCascade.detectMultiScale(gray, 1.3, 5)
#Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = image[y:y+h, x:x+w]
return image
#for video capturing
vidCapture = cv2.VideoCapture(0)
while True:
# To capture video frame by frame
_, image = vidCapture.read()
#monochrome image capturing (color to gray)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
subjects = ["Daksh", "Ishika", "Shivang", "Dashya"]
canvas = imgdetect(gray, image)
gray, rect = detect_face(image)
label = face_recognizer.predict(gray)
label_text = subjects[label]
#draw a rectangle around face detected
draw_rectangle(canvas, rect)
#draw name of predicted person
draw_text(canvas, label_text, rect[0], rect[1]-5)
cv2.imshow("Attendence....", canvas)
#To break control by pressing 'q'
if cv2.waitKey(1) & 0xff == ord('q'):
break
#Release after processing is done
vidCapture.release()
cv2.destroyAllWindows()
But, I am getting the following error while proceeding with the code
---------------------------------------------------------------------------
error Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_13428/371309763.py in <module>
40 canvas = imgdetect(gray, image)
41 img, rect = detect_face(image)
---> 42 label = face_recognizer.predict(img)
43 label_text = subjects[label]
44
error: OpenCV(4.5.4) D:\a\opencv-python\opencv-python\opencv\modules\core\src\matrix.cpp:250: error: (-215:Assertion failed) s >= 0 in function 'cv::setSize'
I am unable to understand the error message.
Why am I getting this error and what can be the possible fixes for the error?
I'm keep running with error in line 8.
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") RuntimeError: Unable to open shape_predictor_68_face_landmarks.da
I downloaded the files I tried to add the files to working directory but PyCharm doesn't let me drag and drop anything.
import cv2
import numpy as np
import dlib
cap = cv2.VideoCapture(0)
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
def midpoint(p1 ,p2):
return int((p1.x + p2.x)/2), int((p1.y + p2.y)/2)
while True:
_, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = detector(gray)
for face in faces:
#x, y = face.left(), face.top()
#x1, y1 = face.right(), face.bottom()
#cv2.rectangle(frame, (x, y), (x1, y1), (0, 255, 0), 2)
landmarks = predictor(gray, face)
left_point = (landmarks.part(36).x, landmarks.part(36).y)
right_point = (landmarks.part(39).x, landmarks.part(39).y)
center_top = midpoint(landmarks.part(37), landmarks.part(38))
center_bottom = midpoint(landmarks.part(41), landmarks.part(40))
hor_line = cv2.line(frame, left_point, right_point, (0, 255, 0), 2)
ver_line = cv2.line(frame, center_top, center_bottom, (0, 255, 0), 2)
cv2.imshow("Frame", frame)
key = cv2.waitKey(1)
if key == 27:
break
cap.release()
cv2.destroyAllWindows()
Solve the problem. I went in my Mac open pycharm install dlib and at first I had the same problem. the type in google shape_predictor_68_face_landmarks.dat. search different option the best source: https://github.com/davisking/dlib-models/blob/master/shape_predictor_68_face_landmarks.dat.bz2 download the file and I dropped the file in sam
I am trying to create an robot that can follow a human that I chose, for that I am usig raspberry pi with python and openCV.
I want to create bbox around a human, and I want my camera to track that human, I found pieces of codes on internet and I tried to put them together but when I start the code it gives me image, I can select an object but it doesn't update the frames and the image is freezed.
It also gives me an error when ii press space or another key:
"ok = tracker.init(image, bbox)
NameError: name 'tracker' is not defined"
Can someone give me some advices?
Here is the code only for object tracking:
from picamera import PiCamera
import time
import cv2
import numpy as np
# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(640, 480))
while True:
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
image = frame.array
bbox = cv2.selectROI(image, False)
ok = tracker.init(image, bbox)
cv2.imshow("Camera Output", image)
#rawCapture.truncate(0)
ok, bbox = tracker.update(image)
fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer)
if ok:
p1 = (int(bbox[0]), int(bbox[1]))
p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
cv2.rectangle(frame, pi, p2, (255, 0, 0), 2, 1)
else:
cv2.putText(image, "Tracking failure detected", (100, 80),
cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
cv2.putText(frame, tracker_type + "Tracker", (100, 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2);
cv2.putText(image, "FPS:" ++ str(int(fps)), (100, 50),
cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2);
cv2.imshow("Tracking", image)
k = cv2.waitKey(5) #& 0xFF
if "q" == chr(k & 255):
break```
So the method tracker is not defined. I found this which initializes the method. YOu can do it like this:
tracker = cv2.TrackerKCF_create()
This is considering that you want to implement opencv function
I am using Raspberry Pi3 for face recognition and this is my code to detect the faces but the real time recognition ran slowly
cam = cv2.VideoCapture(0)
rec = cv2.face.LBPHFaceRecognizer_create();
rec.read(...'/data/recognizer/trainingData.yml')
getId = 0
font = cv2.FONT_HERSHEY_SIMPLEX
userId = 0
i = 0
while (cam.isOpened() and i<91):
i=i+1
ret, img = cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceDetect.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
getId, conf = rec.predict(gray[y:y + h, x:x + w]) # This will predict the id of the face
# print conf;
if conf < 50:
userId = getId
cv2.putText(img, "Detected", (x, y + h), font, 2, (0, 255, 0), 2)
record = Records.objects.get(id=userId)
record.is_present = True
record.save()
else:
cv2.putText(img, "Unknown", (x, y + h), font, 2, (0, 0, 255), 2)
# Printing that number below the face
# #Prams cam image, id, location,font style, color, stroke
cv2.imshow("Face", img)
cv2.waitKey(50)`
How to correct it please ? Thanks for your helping hand.
You should use threads to mazimize performance. imutils is a library that lets you use threading both on picamera and webcam capture. The issue here is that there are too many Input output operations being performed in between frames.
Here is the article that helped increase my fps:
https://www.pyimagesearch.com/2015/12/28/increasing-raspberry-pi-fps-with-python-and-opencv/
And this is the code you can add:
import imutils
from imutils.video.pivideostream import PiVideoStream
Then instead of cam = cv2.VideoCapture(0)
use cam = PiVideoStream().start()
and instead of ret, img = cam.read()
use im = cam.read()
and to release the camera use:
cam.stop()
I has raspberry pi 3 with raspbian jessie installed. Open CV3 and python 3.0 also installed. I get a python sample code which detect the face. I need write some text on the screen but it's not write on it. I need write text once instead of repeated on top of each faces. Below is the code
import cv2
import sys
from tkinter import *
from tkinter import messagebox
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
video_capture = cv2.VideoCapture(0)
video_capture.set(3,500)
video_capture.set(4,300)
video_capture.set(12, 0.1)
frameWidth = int(video_capture.get(cv2.CAP_PROP_FRAME_WIDTH))
frameHeight = int(video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
while True:
# Capture frame-by-frame
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.CASCADE_SCALE_IMAGE
)
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
#cv2.putText(frame, 'How are you', (x - 1, y - 1), cv2.FONT_HERSHEY_PLAIN,2,(0, 255, 0))
#if the puttext at here, it will write the text on top of each face detected. But I just need the text appear once.
# Display the resulting frame
cv2.imshow('Video', frame)
if len(faces) > 0:
cv2.putText(img = frame, text = 'How are you', org = (int(frameWidth/2 - 20),int(frameHeight/2)), fontFace = cv2.FONT_HERSHEY_DUPLEX, fontScale = 3,
color = (0, 255, 0))
#print(int(frameWidth/2 - 20),int(frameHeight/2))
#print('Found ' + str(len(faces)) + ' face(s)')
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
You are calling imshow before putText so you'll never see the results of putText. Move the imshow statement to the line before waitKey.