EN: Object detection in camera streams#7490
Merged
TEParsons merged 12 commits intopsychopy:devfrom Dec 10, 2025
Merged
Conversation
…sychopy into dev-en-opencv-cameras
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## dev #7490 +/- ##
==========================================
- Coverage 48.93% 48.78% -0.16%
==========================================
Files 355 355
Lines 64990 65246 +256
==========================================
+ Hits 31806 31829 +23
- Misses 33184 33417 +233
🚀 New features to boost your workflow:
|
TEParsons
approved these changes
Dec 10, 2025
Contributor
TEParsons
left a comment
There was a problem hiding this comment.
All looks good and seems to be working great! For the record this is the code I used to test it:
from psychopy import visual
from psychopy.tools import imagetools
from psychopy.hardware import camera
import time
# setup camera
for profile in camera.CameraDevice.getAvailableDevices():
cam = camera.Camera(profile['device'])
break
# setup detector
faces = imagetools.HaarCascadeObjectRecognizer('haarcascade_frontalface_default.xml', name='face')
# setup window
win = visual.Window(checkTiming=False)
# setup tracking dot
dot = visual.Rect(win, fillColor="red", units="norm")
# start camera
cam.start()
# start frame loop
start = time.time()
while time.time() - start < 10:
# get camera frames
cam.poll()
# do detection
if cam.lastFrame:
found = cam.lastFrame.detectObjects(faces)
# if we found a face...
if found:
if len(found['face']['objects']):
# get size and position
pos = found['face']['objects'][0]['center']
size = found['face']['objects'][0]['size']
# set dot pos (adjusting for unit space)
dot.pos = [
(i * 2) / total
for i, total in zip(pos, cam.lastFrame.frameSize)
]
# set dot size (adjusting for unit space)
dot.size = [
(i * 2) / total
for i, total in zip(size, cam.lastFrame.frameSize)
]
# draw dot
dot.draw()
# draw to win
win.flip()
# sleep so other stuff can happen
time.sleep(0.01)
# stop camera
cam.stop()
and the dot on screen seemed to move (albeit flipped horizontally) in the same way I moved my head around in front of the camera.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds object detection capabilities to the camera interface. The user can define classifiers and pass them to an object detection method which returns data about any detected objects (position, size, etc.)
This PR also adds a new
CameraFrameobject which encapsulates frame data and contains the newdetectObjects()method. To perform object detection, first create a new classifier:Then pass the classifier to the
detectObjects()method of desired frame to detect objects:Returns the results as such:
This also supports batch object detection which allows the user to specify a set of classifiers and pass them all at once to
detectObjectsas a list. This is more efficient than calling the method multiple times since it avoids multiple color conversions.