0
import cv2
import numpy as np
while True:
    cam = cv2.VideoCapture(0)
    while(cam.isOpened()):
        ret, im = cam.read()
        im=im.reshape(1,-1,3)
        im_list=im.tolist()
        im_tuples=map(tuple,im_list[0])
        im_set=set(im_tuples)
        print len(im_set)

This is my code. It runs really slow (like once a second). How can I increase its speed? It already seems really small. Do I lower the image dimensions or something? Or is this as fast as it gets?

Davey Boy
  • 79
  • 1
  • 10
  • What resolution is your video input? – Dan Mašek May 08 '16 at 18:28
  • Just doing a [simple benchmark](http://pastebin.com/YbbLnYpK) I get timings like `[0.0, 1.2784000396728517, 0.3119999885559082, 0.45780000686645506]`. So the conversion to list is by far the slowest (likely due to creation of ~2 million lists, 1 for each pixel), but the other two functions are not significantly better off. What exactly are you trying to achieve? – Dan Mašek May 08 '16 at 18:42
  • I am going to have multiple cameras operating at once on a vehicle and they turn a switch on when they match up to a certain colour. I want to hit about 30 frames per second. I'm not sure if this is possible... Normal resolution is something like 480 x 640 Can I skip the conversion to list? I also noticed that lowering the image size to about (100, 50) did increase it 5x. But that's a big drop in quality. – Davey Boy May 08 '16 at 19:00
  • I see. The way to speed this up is to move as much into functions that are implemented in C. When I [pack each pixel into an integer](http://pastebin.com/rxtNE169), the whole thing gets a lots faster: `[0.0004000663757324219, 0.02199997901916504, 0.0944000244140625, 0.31279993057250977]`. But still building the set is slow (it's going to be something like O(n log n) complexity and unknown overhead). With RGB you may have 2^24 colours, so the set is bound to be large. Could you use histograms (perhaps in HSV space) to do the matching? – Dan Mašek May 08 '16 at 19:07

0 Answers0