0

I have a simple project with Raspi 4 with camera which the project is similar with car's reverse camera but without sensor. Here my code:

import time
import cv2
import numpy as np
from picamera.array import PiRGBArray
from picamera import PiCamera

camera = PiCamera()
camera.resolution = (1080, 720) # camera resolution 
camera.framerate = 25
rawCapture = PiRGBArray(camera, size=(1080,720))
kernel = np.ones((2,2),np.uint8)
time.sleep(0.1)
for still in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
    
    image = still.array
    #create a detection area
    widthAlert = np.size(image, 1) #get width of image
    heightAlert = np.size(image, 0) #get height of image
    yAlert = (heightAlert/2) + 100 #determine y coordinates for area
    cv2.line(image, (0,yAlert), (widthAlert,yAlert),(0,0,255),2) #draw a line to show area
    
    lower = [1, 0, 20]
    upper = [60, 40, 200]
    lower = np.array(lower, dtype="uint8")
    upper = np.array(upper, dtype="uint8")
    #use the color range to create a mask for the image and apply it to the image
    mask = cv2.inRange(image, lower, upper)
    output = cv2.bitwise_and(image, image, mask=mask)
    
    dilation = cv2.dilate(mask, kernel, iterations = 3)
    closing = cv2.morphologyEx(dilation, cv2.MORPH_GRADIENT, kernel)
    closing = cv2.morphologyEx(dilation, cv2.MORPH_CLOSE, kernel)
    edge = cv2.Canny(closing, 175, 175)
    
    contours, hierarchy = cv2.findContours(closing, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    
    threshold_area = 400
    centres = []
    
    if len(contours) !=0:
        
        for x in contours:
            #find the area of each contour
            area = cv2.contourArea(x)
            #find the center of each contour
            moments = cv2.moments(x)
            #weed out the contours that are less than our threshold
            if area > threshold_area:
                
                (x,y,w,h) = cv2.boundingRect(x)
                
                centerX = (x+x+w)/2
                centerY = (y+y+h)/2
                
                cv2.circle(image,(centerX, centerY), 7, (255, 255, 255), -1)
                
                if ((y+h) > yAlert):
                    cv2.putText(image, "ALERT!", (centerX -20, centerY -20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255),2)
    
    cv2.imshow("Display", image)
    
    rawCapture.truncate(0)
    
    key = cv2.waitKey(1) & 0xFF
    if key == ord("q"):
        break

The error i got is in:

 image = still.array
    #create a detection area
    widthAlert = np.size(image, 1) #get width of image
    heightAlert = np.size(image, 0) #get height of image
    yAlert = (heightAlert/2) + 100 #determine y coordinates for area
    cv2.line(image, (0,yAlert), (widthAlert,yAlert),(0,0,255),2) #draw a line to show area

The problem is: Traceback (most recent call last): File "/home/pi/Desktop/object_detector.py", line 20, in

cv2.line(image, (0,yAlert), (widthAlert,yAlert),(0,0,255),2) #draw a line to show area

cv2.error: OpenCV(4.5.3) error: (-5:Bad argument) in function 'line'

Overload resolution failed:

-can't parse 'pt1'. Sequence item with index 1 has wrong type

-can't parse 'pt1'. Sequence item with index 1 has wrong type

Luqman _
  • 23
  • 1
  • 1
  • 3

2 Answers2

3

The value assigned to pt1 and pt2 should not have a floating point.

So this is working fine.

import cv2
import numpy as np

h,w=100,100
im = ~np.zeros((h,w,3), np.uint8)

cv2.line(im, (0,10), (100,100),(0,0,255),2)
cv2.imshow('line',im)
cv2.waitKey(0)

Now if you change this line

cv2.line(im, (0,10), (100,100),(0,0,255),2)

to this

cv2.line(im, (0,10.1), (100,100),(0,0,255),2)
#OR
cv2.line(im, (0,10), (100,100.1),(0,0,255),2)

For the first one you get

Can't parse 'pt1'. Sequence item with index 1 has a wrong type

and for second one you get

Can't parse 'pt2'. Sequence item with index 1 has a wrong type

To fix this I can change

cv2.line(im, (0,10.1), (100,100),(0,0,255),2)

to

cv2.line(im, (0,int(10.1)), (100,100),(0,0,255),2)
Shamshirsaz.Navid
  • 2,224
  • 3
  • 22
  • 36
1

It seem it must insert 'int' in my code from:

 widthAlert = np.size(image, 1) #get width of image
heightAlert = np.size(image, 0) #get height of image
yAlert = (heightAlert/2) + 100 #determine y coordinates for area
cv2.line(image, (0,yAlert), (widthAlert,yAlert),(0,0,255),2) #draw a line to show area

to:

 widthAlert = np.size(image, 1) #get width of image
heightAlert = np.size(image, 0) #get height of image
yAlert = (heightAlert/2) + 100 #determine y coordinates for area
cv2.line(image, (0,int(yAlert), (widthAlert,yAlert),(0,0,255),2) #draw a line to show area

But then i got same problem in cv2.circle and same solution from my own question xD. Tq @Shamshirsaz.Navid for responding my question, your explaination very helping me.

Luqman _
  • 23
  • 1
  • 1
  • 3