Camera Recorder - fdechaumont/micecraft GitHub Wiki
The purpose of the CameraRecorder is to manage you monitoring webcams. You can use this object to record in a .mp4 format your webcams. Also, the main purpose of this object is to record a specific event to avoid recording all the experiment, ending up with a huge file that nobody will watch. For instance, if you want to capture what is happening around a lever press, you can launch a record in one line of code when you have the lever press event, that will automatically capture the video feed of what happened 5s before and 5s after the event. A timestamp is always displayed in overlay to the capture.
General call.
- deviceNumber is 0 by default, using the first available webcam on the system. Note that you can record the virtual webcam produced by OBS 🥳
- bufferDuration is the number of second of video that will be kept in memory
- showStream displays the window with the current live view + the timestamp overlay. This window cannot be closed by the user.
- filePrefix is a string to add before the auto-generated name for each video
camRecorder = CameraRecorder( deviceNumber = 0, bufferDurationS=20, showStream = True, filePrefix="" )
CameraRecorder Scenarios:
Continuously save the stream of a webcam to a .mp4 file
from micecraft.soft.CameraRecorder.CameraRecorder import CameraRecorder, CRText
# create the CameraRecorder object
camRecorder = CameraRecorder( deviceNumber = 0, bufferDurationS=20, showStream = True )
# save the stream
camRecorder.saveStream( )
# wait 10 seconds for demo purpose
sleep( 10 )
# stop recording the stream
camRecorder.stopStream( )
Save the last 5 seconds of the stream, and add an overlay text in the video
import datetime
from micecraft.soft.CameraRecorder.CameraRecorder import CameraRecorder, CRText
# create the CameraRecorder object
camRecorder = CameraRecorder( deviceNumber = 0, bufferDurationS=20, showStream = True )
# create a text that we want to add as an overlay over the video.
text = CRText( "recorded 5 seconds before call to 5 seconds after call", 10, 10, fontScale=0.5,centerX = True )
camRecorder.delayedSave( 5, minDateTime= datetime.datetime.now() - datetime.timedelta(seconds=5), textList =[text] )
Call the CameraRecorder to save the last 5s up to the next 5s
... so that you can see what happened before and after the event.
import datetime
from micecraft.soft.CameraRecorder.CameraRecorder import CameraRecorder, CRText
# create the CameraRecorder object
camRecorder = CameraRecorder( deviceNumber = 0, bufferDurationS=20, showStream = True )
text = CRText( "recorded 5 seconds before call to 5 seconds after call", 10, 10, fontScale=0.5,centerX = True )
camRecorder.delayedSave( 5, minDateTime= datetime.datetime.now() - datetime.timedelta(seconds=5), textList =[text] )
Setup multiple cameras
In this example we monitor 3 cameras
import datetime
from micecraft.soft.CameraRecorder.CameraRecorder import CameraRecorder, CRText
## create the CameraRecorder object
camRecorder1 = CameraRecorder( deviceNumber = 0, bufferDurationS=20, showStream = True )
camRecorder2 = CameraRecorder( deviceNumber = 1, bufferDurationS=20, showStream = True )
camRecorder3 = CameraRecorder( deviceNumber = 2, bufferDurationS=20, showStream = True )
Record specific windows and a complex layout of webcam
For that purpose, best is to use OBS. With this software, you can create a complex layout that can be grabbed by the CameraRecorder if you setup the Virtual Camera. This will add a virtual camera with the feed you created in OBS that can be recorder in the CameraRecorder.
Customize your text overlays:
Here is a sample code where you can set the color of the text and its background
camRecorder.addText( CRText( "my text", randint(0,640),randint(0,480), centered=True, color = (randint(0,255),randint(0,255),randint(0,255)) , bgColor= (randint(0,255),randint(0,255),randint(0,255) ) ) )
A text overlay can be displayed in only a given time interval over the video. For instance if you want to show a message at the exact moment the animal pressed a lever.
text = CRText( "this will appear 1s before call, up to 1s after call" , 10 , 10 , centered=False , color=(0,0,255), showBackGround = False, minDateTime= datetime.datetime.now() - datetime.timedelta(seconds=1) , maxDateTime = datetime.datetime.now() + datetime.timedelta(seconds=1) )
camRecorder.delayedSave( 5, minDateTime= datetime.datetime.now() - datetime.timedelta(seconds=5), textList =[text] )
stop the monitoring:
camRecorder.shutdown()
bind camera to an object emitting DeviceEvent object
if you want to display events coming from a device, just use:
camRecorder.bindDeviceToListen( myDevice )
More examples
look at cameraRecordDemo.py for an interactive demo