I wrote a simple program for Maemo by Python to check some pixel's color every time that my function is called. But this function runs very slowly (3-5 seconds each call). Is there any faster way to do this?
import Image
import os
import sys
# sen_pos = (pixel_x, pixel_y)
def sen(sen_pos):
os.system("gst-launch v4l2src device=/dev/video0 num-buffers=1 ! ffmpegcolorspace ! jpegenc ! filesink location=cam.jpg")
frame = Image.open("cam.jpg")
col = frame.getpixel((sen_pos[0], sen_pos[1]))
avecol = sum(col) / len(col)
if avecol > 127:
return "white"
elif avecol < 127:
return "black"
return None
The process of using a phone camera with Python: First, install the OpenCV library in Python; pip install opencv-python. Download and install the IP Webcam application on your smartphones. After installing the IP Webcam app, make sure your phone and PC are connected to the same network.
if camlist: # Initialize and start camera cam = pygame.camera.Camera (camlist [0], (640, 480)) cam.start () # capturing the single image image = cam.get_image () # saving the image pygame.image.save (image, "filename.jpg") else: if camera is not detected the moving to this part
1 Make sure the following Python packages are installed: numpy, matplotlib, opencv-python, comtypes. 2 Run main.py. 3 Once the application is started, the dialog Select a video device is shown. Select a camera, then press Ok.
PyGame.camera () camera initializer supports only Linux operating system and Currently, It is not compatible with Windows. To install PyGame in Linux, Enter the below command on the Linux terminal. 1. Import pygame.camera module 2.
Calling an external program via os.system
is likely what's taking the time.
Try using GStreamer Python Bindings instead and keeping the video object around between calls. The docs for Videomixer may help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With