Uraforclain
Posts: 5
Joined: Wed Jan 17, 2018 10:32 am

Using RaspberryPi 3 with Pi-Camera and OpenCV to do PeopleCounting From the Top

Wed Jan 17, 2018 10:42 am

I have been stuck for sometime now. Does anyone know of any links that can help me with using

Raspberry Pi 3 connected to a Pi Cam NOT webcam

Then using both hardware mentioned above i wan to use any available software I'm guessing openCV to do people counting from the top.

Example Video: https://www.youtube.com/watch?v=BszUJXLR2oA

Almost all available examples using the raspberry pi to do people counting from the top doesnt use the picam.. webcams are big and bulky. So if there is any tutorial or what available please help. Thank You

supra
Posts: 855
Joined: Thu Feb 28, 2013 9:21 am
Location: Ontario, Canada

Re: Using RaspberryPi 3 with Pi-Camera and OpenCV to do PeopleCounting From the Top

Thu Jan 18, 2018 3:16 am

I attempted this link https://www.hackster.io/phfbertoleti/co ... ncv-015ba5 and it worked for me with webcam. U can attempt with picamera. It will work from top too

Uraforclain
Posts: 5
Joined: Wed Jan 17, 2018 10:32 am

Re: Using RaspberryPi 3 with Pi-Camera and OpenCV to do PeopleCounting From the Top

Thu Jan 18, 2018 4:40 am

Actually i attempted this
https://www.hackster.io/deligence-techn ... hon-faf14f

It works with my USB webcam but i have no idea how to use the picam as the input :(

This is the code for the programme that i want which works with normal USB cam

Code: Select all

import argparse
import datetime
import imutils
import math

import cv2
import numpy as np

width = 800

textIn = 0
textOut = 0

def testIntersectionIn(x, y):

    res = -450 * x + 400 * y + 157500
    if((res >= -550) and  (res < 550)):
        print (str(res))
        return True
    return False



def testIntersectionOut(x, y):
    res = -450 * x + 400 * y + 180000
    if ((res >= -550) and (res <= 550)):
        print (str(res))
        return True

    return False

camera = cv2.VideoCapture(0)

firstFrame = None


while True:

    (grabbed, frame) = camera.read()
    text = "Unoccupied"


    if not grabbed:
        break

    frame = imutils.resize(frame, width=width)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (21, 21), 0)


    if firstFrame is None:
        firstFrame = gray
        continue


    frameDelta = cv2.absdiff(firstFrame, gray)
    thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]

    thresh = cv2.dilate(thresh, None, iterations=2)
    _, cnts, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    for c in cnts:
        if cv2.contourArea(c) < 12000:
            continue

        (x, y, w, h) = cv2.boundingRect(c)
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

        cv2.line(frame, (width / 2, 0), (width, 450), (250, 0, 1), 2) #blue line
        cv2.line(frame, (width / 2 - 50, 0), (width - 50, 450), (0, 0, 255), 2)#red line


        rectagleCenterPont = ((x + x + w) /2, (y + y + h) /2)
        cv2.circle(frame, rectagleCenterPont, 1, (0, 0, 255), 5)

        if(testIntersectionIn((x + x + w) / 2, (y + y + h) / 2)):
            textIn += 1

        if(testIntersectionOut((x + x + w) / 2, (y + y + h) / 2)):
            textOut += 1

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

    cv2.putText(frame, "In: {}".format(str(textIn)), (10, 50),
                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
    cv2.putText(frame, "Out: {}".format(str(textOut)), (10, 70),
                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
    cv2.putText(frame, datetime.datetime.now().strftime("%A %d %B %Y %I:%M:%S%p"),
                (10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 255), 1)
    cv2.imshow("Security Feed", frame)

camera.release()
cv2.destroyAllWindows()

And this is the code just to simply use picam to stream a video

Code: Select all

# import the necessary packages
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
 
# 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))
 
# allow the camera to warmup
time.sleep(0.1)
 
# capture frames from the camera
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
	# grab the raw NumPy array representing the image, then initialize the timestamp
	# and occupied/unoccupied text
	image = frame.array
 
	# show the frame
	cv2.imshow("Frame", image)
	key = cv2.waitKey(1) & 0xFF
 
	# clear the stream in preparation for the next frame
	rawCapture.truncate(0)
 
	# if the `q` key was pressed, break from the loop
	if key == ord("q"):
		break


Now the problem is i have no idea how to merge both of them together

supra
Posts: 855
Joined: Thu Feb 28, 2013 9:21 am
Location: Ontario, Canada

Re: Using RaspberryPi 3 with Pi-Camera and OpenCV to do PeopleCounting From the Top

Thu Jan 18, 2018 2:20 pm

You simply do wrong way.
Follow this example: http://www.linuxcircle.com/2015/05/24/o ... erry-pi-2/
Btw, I don't have for picamera. And also I don't used 3rd party.

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5081
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Using RaspberryPi 3 with Pi-Camera and OpenCV to do PeopleCounting From the Top

Thu Jan 18, 2018 3:43 pm

Code: Select all

sudo modprobe bcm2835-v4l2
will load a V4L2 driver for the Pi camera module, and then everything that works for your webcam should work on the Pi camera.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
Please don't send PMs asking for support - use the forum.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

Uraforclain
Posts: 5
Joined: Wed Jan 17, 2018 10:32 am

Re: Using RaspberryPi 3 with Pi-Camera and OpenCV to do PeopleCounting From the Top

Fri Jan 19, 2018 7:18 am

Hey @6by9 i tried that before yes it works. However, its very laggy 4 seconds to update what i am actually seeing thus i want to use the picamera library in hope it would be less laggy?

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5081
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Using RaspberryPi 3 with Pi-Camera and OpenCV to do PeopleCounting From the Top

Fri Jan 19, 2018 10:04 am

Uraforclain wrote:
Fri Jan 19, 2018 7:18 am
Hey @6by9 i tried that before yes it works. However, its very laggy 4 seconds to update what i am actually seeing thus i want to use the picamera library in hope it would be less laggy?
Have you profiled where the time is being spent?
In your code

Code: Select all

    (grabbed, frame) = camera.read()
is actually grabbing a frame from the camera. How much time does that take compared to processing the frame?

I also see nothing setting the camera resolution in that code. Try adding

Code: Select all

camera.set(CAP_PROP_FRAME_WIDTH, 640)
camera.set(CAP_PROP_FRAME_HEIGHT, 480)
after "camera = cv2.VideoCapture(0)".
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
Please don't send PMs asking for support - use the forum.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

Uraforclain
Posts: 5
Joined: Wed Jan 17, 2018 10:32 am

Re: Using RaspberryPi 3 with Pi-Camera and OpenCV to do PeopleCounting From the Top

Sat Jan 20, 2018 5:30 am

ok I tried,

Code: Select all

camera.set(CAP_PROP_FRAME_WIDTH, 640)
camera.set(CAP_PROP_FRAME_HEIGHT, 480)
But i get error:
NameError: name 'CAP_PROP_FRAME_WIDTH' is not defined

and if i try

Code: Select all

camera.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
I also get error but it is:
AttributeError: 'module' object has no attribute 'CAP_PROP_FRAM_WIDTH'


Could it be cause I'm not using a legit USB webcam but a piCam and forcing it to believe its a usb webcam via sudo modprobe bcm2835-v4l2

supra
Posts: 855
Joined: Thu Feb 28, 2013 9:21 am
Location: Ontario, Canada

Re: Using RaspberryPi 3 with Pi-Camera and OpenCV to do PeopleCounting From the Top

Sun Jan 21, 2018 1:25 am

Code: Select all

camera = PiCamera()
camera.resolution = (640, 480)
Read the link that I posted. This is meley for picamera not webcam.

Uraforclain
Posts: 5
Joined: Wed Jan 17, 2018 10:32 am

Re: Using RaspberryPi 3 with Pi-Camera and OpenCV to do PeopleCounting From the Top

Sun Jan 21, 2018 4:26 am

ok guys look.

Supra you are asking me to use the picamera library but i have tried to merge both codes and i came up with this

Code: Select all

# import the necessary packages
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
import numpy as np


import argparse
import datetime
import imutils
import math


width = 800

textIn = 0
textOut = 0

firstFrame = None


def testIntersectionIn(x, y):

    res = -450 * x + 400 * y + 157500   #450 400 157500
    if((res >= -550) and  (res < 550)):
        print (str(res))
        return True
    return False



def testIntersectionOut(x, y):
    res = -450 * x + 400 * y + 180000
    if ((res >= -550) and (res <= 550)):
        print (str(res))
        return True
    return False
 
# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 50
camera.hflip = True

rawCapture = PiRGBArray(camera, size=(640, 480))
 
# allow the camera to warmup
time.sleep(0.1)
 
# capture frames from the camera
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):

        (grabbed, frame) = frame.array     			#This is the part that it has error
        text = "Unoccupied"


        if not grabbed:
            break

        frame = imutils.resize(frame, width=width)
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        gray = cv2.GaussianBlur(gray, (21, 21), 0)


        if firstFrame is None:
            firstFrame = gray
            continue


        frameDelta = cv2.absdiff(firstFrame, gray)
        thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]

        thresh = cv2.dilate(thresh, None, iterations=2)
        cnts, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

        for c in cnts:
            if cv2.contourArea(c) < 12000:
                continue

            (x, y, w, h) = cv2.boundingRect(c)
            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

            cv2.line(frame, (width -15500, 0), (width , 300), (250, 0, 1), 2)  #blue line
            cv2.line(frame, (width -15500, 0), (width , 400), (0, 0, 255), 2) #red line


            rectagleCenterPont = ((x + x + w) /2, (y + y + h) /2)
            cv2.circle(frame, rectagleCenterPont, 1, (0, 0, 255), 5)

            if(testIntersectionIn((x + x + w) / 2, (y + y + h) / 2)):
                textIn += 1

            if(testIntersectionOut((x + x + w) / 2, (y + y + h) / 2)):
                textOut += 1

        # clear the stream in preparation for the next frame
        rawCapture.truncate(0)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

        cv2.putText(frame, "In: {}".format(str(textIn)), (10, 50),
                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
        cv2.putText(frame, "Out: {}".format(str(textOut)), (10, 70),
                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
        cv2.putText(frame, datetime.datetime.now().strftime("%A %d %B %Y %I:%M:%S%p"),
                (10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 255), 1)
        cv2.imshow("Counter", frame)







Find the line which i commented #This is the part that it has error ... And the error message is

Code: Select all

Traceback (most recent call last):
  File "Final.py", line 54, in <module>
    (grabbed, frame) = frame.array
ValueError: too many values to unpack





So that is why i tried to use what @6by9 did which was to use sudo modprobe bcm2835-v4l2 to make the pi think that my picam is a USB cam but its just too laggy.. so he suggested to change the width and height.. which i cant i get the errors i mentioned above in my previous post.

Return to “Camera board”

Who is online

Users browsing this forum: No registered users and 3 guests