Ask Your Question
-1

Python type casting [closed]

asked 2020-06-27 00:54:55 -0600

Danny_00 gravatar image

Good day gents,

I got code from the net at this page for simple face recognition. I am playing around with it at the moment like adding text and circles etc.

I want to find the center point of each rectangle (face) that is drawn on the frame. However I keep on getting "

TypeError: cannot concatenate 'str' and 'int' objects

Here is my code:

import cv2
import math

# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

# To capture video from webcam. 
cap = cv2.VideoCapture(0)
# To use a video file as input 
# cap = cv2.VideoCapture('filename.mp4')
#Text Stuff
rectangleThicknes = 1
videoCenterCircleColor = (0, 255, 0)
videoCenterCircleThickness = 1
rectangleCenterCircleColour = (255, 0, 0)
rectangleCenterCircleThickness = 1

while True:
    # Read the frame
    _, img = cap.read()
    #Draw a circle in the middle of the video
    circle = cv2.circle(img, (320, 210), 5, videoCenterCircleColor, videoCenterCircleThickness)
    # Convert to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # Detect the faces
    faces = face_cascade.detectMultiScale(gray, 1.1, 4)
    # Draw the rectangle around each face
    for (x, y, w, h) in faces:
        ###Start finding "x, y, w, h"###
        rectangleXposition = format(x)
        rectangleYposition = format(y)
        rectangleWidth = format(w)
        rectangleHeight = format(h)
        rectangleHalfWidth = int(rectangleWidth) / 2
        rectangleHalfHeight = int(rectangleHeight) / 2
        rectangleCenterXaxis = format(rectangleXposition + rectangleHalfWidth)
        rectangleCenterYaxis = format(rectangleYposition + rectangleHalfHeight)
        ###End finding "x, y, w, h"###

        #Draw a circle in the center of the rectangle around the face
        circle = cv2.circle(img, (rectangleCenterXaxis, rectangleCenterYaxis), 5, rectangleCenterCircleColour, rectangleCenterCircleThickness)
        cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), rectangleThicknes)
    # Display
    cv2.imshow('img', img)
    # Stop if escape key is pressed
    k = cv2.waitKey(30) & 0xff
    if k==27:
        break
# Release the VideoCapture object
cap.release()
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by supra56
close date 2020-06-27 07:30:17.588923

Comments

please lookup how format() works, most likely you did not even want it there

berak gravatar imageberak ( 2020-06-27 01:35:43 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2020-06-27 04:36:16 -0600

supra56 gravatar image

updated 2020-06-27 05:20:25 -0600

You can't used format in cv2.rectangle and cv2.circle. Format is for merely print or cv2.putText. Noticed, I got an error stating that TypeError: integer argument expected, got float. So I added slash. Here is code:

import cv2

# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

# To capture video from webcam. 
cap = cv2.VideoCapture(0)
# To use a video file as input 
# cap = cv2.VideoCapture('filename.mp4')
#Text Stuff
rectangleThicknes = 1
videoCenterCircleColor = (0, 255, 0)
videoCenterCircleThickness = 1
rectangleCenterCircleColour = (255, 0, 0)
rectangleCenterCircleThickness = 1

while True:
    # Read the frame
    _, img = cap.read()
    #Draw a circle in the middle of the video
    circle = cv2.circle(img, (320, 210), 5, videoCenterCircleColor, videoCenterCircleThickness)
    # Convert to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # Detect the faces
    faces = face_cascade.detectMultiScale(gray, 1.1, 4)
    # Draw the rectangle around each face
    for (x, y, w, h) in faces:
        ###Start finding "x, y, w, h"###
        rectangleXposition = (x)
        rectangleYposition = (y)
        rectangleWidth = (w)
        rectangleHeight = (h)
        rectangleHalfWidth = int(rectangleWidth) // 2
        rectangleHalfHeight = int(rectangleHeight) // 2
        rectangleCenterXaxis = int(rectangleXposition + rectangleHalfWidth)
        rectangleCenterYaxis = int(rectangleYposition + rectangleHalfHeight)
        ###End finding "x, y, w, h"###

        #Draw a circle in the center of the rectangle around the face
        circle = cv2.circle(img, (rectangleCenterXaxis, rectangleCenterYaxis), 5, rectangleCenterCircleColour, rectangleCenterCircleThickness)
        cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), rectangleThicknes)
    # Display
    cv2.imshow('img', img)
    # Stop if escape key is pressed
    k = cv2.waitKey(30) 
    if k is 27:
        break
# Release the VideoCapture object
cap.release()
cv2.destroyAllWindows()
edit flag offensive delete link more

Comments

1

Thank you Sir. Your code worked. And thank you for taking the time to reply.

Danny_00 gravatar imageDanny_00 ( 2020-06-27 07:20:41 -0600 )edit

Glad I could help.

supra56 gravatar imagesupra56 ( 2020-06-27 07:52:28 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-06-27 00:54:55 -0600

Seen: 537 times

Last updated: Jun 27 '20