BramWerbrouck
Posts: 4
Joined: Fri Jul 13, 2018 6:18 am

PyImageSearch - Green Ball Tutorial

Mon Aug 06, 2018 9:00 am

Hi,

I followed the instructions from PyImageSearch.com to install OpenCV and want to try the "green ball" tutorial.

but I have an error:

Code: Select all

[INFO]: waiting for the camera to warmup...
Traceback (most recent call last):
 File "pi_alarm.py", line 28, in <module>
   vs = VideoStream(usePiCamera=args["picamera"] > 0).start()
 File "/home/pi/.virtualenvs/cv/lib/python3.5/site-packages/imutils/video/videostream.py", line 13, in __init__
   from .pivideostream import PiVideoStream
 File "/home/pi/.virtualenvs/cv/lib/python3.5/site-packages/imutils/video/pivideostream.py", line 2, in <module>
   from bicamera.array import PiRGBArray
ImportError: No module names 'picamera'
this is the code:

Code: Select all

# import the necessary packages
from __future__ import print_function
from imutils.video import VideoStream
import argparse
import imutils
import time
import cv2
import RPi.GPIO as GPIO

# initialize GPIO
redLed = 21
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(redLed, GPIO.OUT)

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-p", "--picamera", type=int, default=-1,help="whether or not the Raspberry Pi camera should be used")
args = vars(ap.parse_args())

# initialize the video stream and allow the camera sensor to warmup
print("[INFO] waiting for camera to warmup...")
vs = VideoStream(usePiCamera=args["picamera"] > 0).start()
time.sleep(2.0)

# define the lower and upper boundaries of the "green"
# ball in the HSV color space
greenLower = (29, 86, 6)
greenUpper = (64, 255, 255)

# Start with LED off
GPIO.output(redLed, GPIO.LOW)
ledOn = False

# loop over the frames from the video stream
while True:
	# grab the next frame from the video stream, Invert 180o, resize the
	# frame, and convert it to the HSV color space
	frame = vs.read()
	frame = imutils.resize(frame, width=500)
	frame = imutils.rotate(frame, angle=180)
	hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

	# construct a mask for the color "green", then perform
	# a series of dilations and erosions to remove any small
	# blobs left in the mask
	mask = cv2.inRange(hsv, greenLower, greenUpper)
	mask = cv2.erode(mask, None, iterations=2)
	mask = cv2.dilate(mask, None, iterations=2)

	# find contours in the mask and initialize the current
	# (x, y) center of the ball
	cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
		cv2.CHAIN_APPROX_SIMPLE)
	cnts = cnts[0] if imutils.is_cv2() else cnts[1]
	center = None

	# only proceed if at least one contour was found
	if len(cnts) > 0:
		# find the largest contour in the mask, then use
		# it to compute the minimum enclosing circle and
		# centroid
		c = max(cnts, key=cv2.contourArea)
		((x, y), radius) = cv2.minEnclosingCircle(c)
		M = cv2.moments(c)
		center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))

		# only proceed if the radius meets a minimum size
		if radius > 10:
			# draw the circle and centroid on the frame,
			# then update the list of tracked points
			cv2.circle(frame, (int(x), int(y)), int(radius),
				(0, 255, 255), 2)
			cv2.circle(frame, center, 5, (0, 0, 255), -1)

			# if the led is not already on, turn the LED on
			if not ledOn:
				GPIO.output(redLed, GPIO.HIGH)
				ledOn = True

	# if the ball is not detected, turn the LED off
	elif ledOn:
		GPIO.output(redLed, GPIO.LOW)
		ledOn = False

	# show the frame to our screen
	cv2.imshow("Frame", frame)
	key = cv2.waitKey(1) & 0xFF

	# if the 'q' key is pressed, stop the loop
	if key == ord("q"):
		break

# do a bit of cleanup
GPIO.cleanup()
cv2.destroyAllWindows()
vs.stop()
What did I do wrong? Can anyone help me?

User avatar
B.Goode
Posts: 8481
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: PyImageSearch - Green Ball Tutorial

Mon Aug 06, 2018 9:25 am

Can anyone help me?


It might help if you specify what Operating System is running on your RPi.

It seems you are running this script in a python virtual environment (venv)

Is the standard picamera module accessible from that environment?

To check, within that venv, just start the Python3 interpreter and try

Code: Select all

from picamera import PiCamera

(I am also a little puzzled by the reference to 'bicamera', but I am not familiar with the tutorial or library you are using so this might be completely normal... )

texy
Forum Moderator
Forum Moderator
Posts: 5157
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: PyImageSearch - Green Ball Tutorial

Mon Aug 06, 2018 10:55 am

B.Goode wrote:
Mon Aug 06, 2018 9:25 am
Can anyone help me?


It might help if you specify what Operating System is running on your RPi.

It seems you are running this script in a python virtual environment (venv)

Is the standard picamera module accessible from that environment?

To check, within that venv, just start the Python3 interpreter and try

Code: Select all

from picamera import PiCamera

(I am also a little puzzled by the reference to 'bicamera', but I am not familiar with the tutorial or library you are using so this might be completely normal... )
I think you mean 'picamera' - https://www.raspberrypi.org/documentati ... /README.md

Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

User avatar
B.Goode
Posts: 8481
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: PyImageSearch - Green Ball Tutorial

Mon Aug 06, 2018 6:05 pm

I think you mean 'picamera'
The attribution is unclear. But no, that's not what I meant. I was querying this extract from the traceback -

"/home/pi/.virtualenvs/cv/lib/python3.5/site-packages/imutils/video/pivideostream.py", line 2, in <module>
from bicamera.array import PiRGBArray
That seems to be from inside an installed module?

Return to “Python”