Detect Circles in Raspberry pi with PiCamera - python

I want the Raspberry pi B+ to detect circles in images. I've been looking for some code, and I tried to use them with the raspberry pi. Here is my conclusion code, but the mainly problem is that it doesn't get the image with the detected objects (I am using as an example a tennis ball), it just get the image without the draw circle and rectangle.
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
import os
import numpy as np
os.system('sudo modprobe bcm2835-v4l2')
h=200
w=300
camera = PiCamera()
camera.resolution = (w, h)
camera.framerate = 24
rawCapture = PiRGBArray(camera, size=(w, h))
time.sleep(0.1)
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
image_RGB = frame.array
copy_RGB = image_RGB.copy()
grey = cv2.cvtColor(image_RGB, cv2.COLOR_BGR2GRAY)
img_circles = None
img_circles = cv2.HoughCircles(grey, cv2.cv.CV_HOUGH_GRADIENT, 1.2, 100)
if img_circles is not None:
img_circles = np.round(img_circles[0, :]).astype("int")
for (x, y, r) in img_circles:
cv2.circle(copy_RGB, (x, y), r, (0, 255, 0), 4)
cv2.rectangle(copy_RGB, (x - 5, y - 5),(x + 5, y + 5), (0, 128, 255, -1))
cv2.imshow("Copy with Detected Object", copy_RGB)
key = cv2.waitKey(1) & 0xFF
rawCapture.truncate(0)
if key == ord("q"):
break
Any Help is appreciated.

Related

Using hand pose coordinates (landmarks) from python in Blender for visualisation

I am doing a project to visualise hand movement from a wearable device (gloves) to a screen. I am using Mediapipe to get hand landmarks. This is to get synthetic data for my project. Now I am planning to use Blender to visualise and animate the hand movement. My questions are as follows:
Is there any alternatives for this method?
How to I import these points (as a csv file)and map them on to my blender image?
Note: I am a novice in both Python and Blender.
Any and all help would be appreciated . Thanks in advance.
I used Mediapipe to get the coordinates.
my code for Mediapipe:
'''
import cv2
import mediapipe as mp
import time
import uuid
import os
import numpy as np
cap = cv2.VideoCapture(0)
mpHands = mp.solutions.hands
hands = mpHands.Hands(max_num_hands=1)
mpDraw = mp.solutions.drawing_utils
pTime = 0
cTime = 0
while True:
success, img = cap.read()
#img = cv2.resize(img, (680,420))
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
imgRGB.flags.writeable = False
results = hands.process(imgRGB)
imgRGB.flags.writeable = True
imgRGB = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
print(results.multi_hand_landmarks)
if results.multi_hand_landmarks:
for handLms in results.multi_hand_landmarks:
for id, lm in enumerate(handLms.landmark):
#to get width and height of the image
h, w, c = img.shape
#to get center points
cx,cy = int(lm.x*w), int(lm.y*h)
print(id, cx, cy)
#lms= lm.append([id, cx, cy])
if id == 0:
cv2.circle(img, (cx,cy), 15, (255,0,255),cv2.FILLED)
mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS,
mpDraw.DrawingSpec(color=(201,122,76), thickness=2, circle_radius=2),
mpDraw.DrawingSpec(color=(121,44,250), thickness=4, circle_radius=4),)
cTime = time.time()
fps = 1/(cTime-pTime)
pTime = cTime
cv2.putText(img, str(int(fps)), (10,70), cv2.FONT_HERSHEY_PLAIN, 3, (255 ,125,0),2)
#cv2.imwrite(os.path.join('Output Images', '{}.jpg'.format(uuid.uuid1())),img)
cv2.imshow('Image', img)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
''''

why does the haarcascades does not work on opencv

I am trying to detect faces in opencv,but I'm running in some issues:
1-When I put the following syntax:gray = cv2.cvtColor(frames,cv2.COLOR_BGR2GRAY),it shows up in red and does not work.
2-The haarcascade also shows up in red:faces = face_cascade.detectMultiScale( gray,scaleFactor=1.1,minNeighbors=5,minSize=(30, 30),flags = cv2.CV_HAAR_SCALE_IMAGE).I tried to do like in some tutorials but it does not work.Would you have any idea?
Here is my code:
#importing packages
import cv2
import numpy as np
#variables
webcam = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
w_size = (700,500)
#turn the webcam on
while (True):
#reading camera and turing into frames
ret,frames = webcam.read()
frames = cv2.resize(frames,w_size)
#detection
gray = cv2.cvtColor(frames,cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale( gray,scaleFactor=1.1,minNeighbors=5,minSize=(30, 30),flags = cv2.CV_HAAR_SCALE_IMAGE)
for (x, y, w, h) in faces:
cv2.rectangle(frames, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imshow('face_recognition',frames)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
#running script
webcam.release()
cv2.destroyAllWindows()
i simply:
added cv2.data.haarcascades as prefix of the type CascadeClassifier
deleted cv2.CV_HAAR_SCALE_IMAGE (parameter not used anymore)
Code:
import cv2
import numpy as np
import cv2.data
#variables
webcam = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades+'haarcascade_frontalface_default.xml')
w_size = (700,500)
#turn the webcam on
while (True):
#reading camera and turing into frames
ret,frames = webcam.read()
frames = cv2.resize(frames,w_size)
#detection
gray = cv2.cvtColor(frames,cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale( gray,scaleFactor=1.1,minNeighbors=5,minSize=(30, 30))
for (x, y, w, h) in faces:
cv2.rectangle(frames, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imshow('face_recognition',frames)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
#running script
webcam.release()
cv2.destroyAllWindows()

OpenCV face recognition is too slow

I tried to use OpenCV with face_recognition to get real-time face recognition, but it end up being slow and laggy. Any help please?
Code:
import face_recognition
import os
import cv2
from PIL import Image
appdata = os.getenv('APPDATA') + "/Project/"
face_cascade = cv2.CascadeClassifier((appdata + "data/face_cascade.xml").replace("\\", "/"))
cap = cv2.VideoCapture(0) #Capture video (OpenCV)
def Main():
while True: #OpenCV start video capture from webcam
ret, frame = cap.read()
gray_scale = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray_scale, 1.5, 5)
for x, y, w, h in faces:
#Start face recognition (face_recognition)
roi_color = frame[y: (y + h) - 15, x: (x + w) - 15] #This cuts the background leaving only the face
cv2.rectangle(frame, (x, y), ((x + w) - 15, (y + h) - 15), (255, 35, 36), 2)
face_encodings = face_recognition.face_encodings(roi_color) #This encodes face features and causes lag
cv2.imshow('sysPy', frame)
if cv2.waitKey(20) == 27:
break
cap.release()
cv2.destroyAllWindows()
Main()
Also I was thinking about "face_encodings = face_recognition.face_encodings(roi_color)" to not encode every frame, skip frames to reduce the lag. Thanks!

Opencv, Python and raspberrypi3

I'm making smartmirror with opencv. I use raspbian but, I have a problem.
Import the necessary packages
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
def detect(img, cascade):
rects = cascade.detectMultiScale(img, scaleFactor=1.3, minNeighbors=4,
minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE)
if len(rects) == 0:
return []
rects[:,2:] += rects[:,:2]
return rects
def draw_rects(img, rects, color):
for x1, y1, x2, y2 in rects:
cv2.rectangle(img, (x1, y1), (x2, y2), color, 2)
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))
cascade = cv2.CascadeClassifier("opencv-3.3.0/data/haarcascades/haarcascade_frontalface_alt.xml")
Allow the camera to warmup
time.sleep(0.1)
scan = 0
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
img = frame.array
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.equalizeHist(gray)
rects = detect(gray, cascade)
if len(rects) != 0:
scan = 1
vis = img.copy()
draw_rects(vis, rects, (0, 255, 0))
# show the frame
cv2.imshow("Frame", vis)
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 scan == 1:
break
so,
In a virtual environment, I want to take variable(scan) into my file(.py).
but, I get an error, No module named cv2
how can I use the variable(scan) into my file??? help me, please.
This is my file(.py)
#smartmirror.py
from __future__ import print_function
from aplclient.discovery import build
from httplib2 import Http
from oauth2client import file, clinet, tools
from Tkinter import *
import locale
(used coding)

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

I have been stuck for sometime now. Does anyone know of any links that can help me with using
(Hardware)- 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
==========================================================================
What i tried:
So the problem I'm having is i have a sample code that uses openCV with a webcam.. instructions can be found here :https://www.hackster.io/deligence-technologies/person-counting-system-using-opencv-and-python-faf14f
And in this code it uses a usb webcam thus the line that i commented that says "#HERE i need to use the pi cam instead" that line is using cv2.VideoCapture(0).. i need to know how to use the picam instead. any ideas?
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) #HERE i need to use the pi cam instead
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()
You say you read the link I posted in my comment but that is obviously not the case.
For clarity, this tutorial shows you how to do what you want to do and you need to read the code in the article and bring that into your code base.
What you are trying to do in your code is open the first USB webcam attached to your raspberry Pi. You do that on this line here:
camera = cv2.VideoCapture(0) #HERE i need to use the pi cam instead
As your comment does indeed state.
What you need to do is instead use the PiCamera library, like this:
# 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()
rawCapture = PiRGBArray(camera)
# allow the camera to warmup
time.sleep(0.1)
# grab an image from the camera
camera.capture(rawCapture, format="bgr")
image = rawCapture.array
# display the image on screen and wait for a keypress
cv2.imshow("Image", image)
cv2.waitKey(0)
The above example and tutorial should get you up and running with the basics, you can then modify the Hackster tutorial that you are following to use the PiCamera instead.

Categories