Pics Taken At Different Times Are The Same

I have a program that uses OpenCV to detect a passing train and its direction. I tweet an alert so subscribers know a train is coming - and I include a picture of the train as a reply to the initial tweet. This has been running fine for a couple of years. (Freight trains are very disruptive in the town where I live.)

I want to get a “better” train image to tweet - which means I need to wait a few seconds after the train is detected - to enable more of the train to enter the frame.

After the train detection logic I have the following code:

ret, pic1 =
pic1_cropped = pic1[picy:picy + picyheight, picx:picx + picxwidth]
cv2.imwrite("C:\\TrainAlertImages\\Pic1.jpg", pic1_cropped)
print("Time before sleep:  %s" % time.ctime())
if direction == "SOUTHBOUND":
print("Time after sleep:  %s" % time.ctime())
ret, pic2 =
pic2_cropped = pic2[picy:picy + picyheight, picx:picx + picxwidth]
cv2.imwrite("C:\\TrainAlertImages\\Pic2.jpg", pic2_cropped)

Both Pic1 and Pic2 are exactly the same. The sleep function appears to be working correctly based on the output in the log file from the print statements.

The camera is an Amcrest bullet camera. No apparent issues with it. It can deliver images to my code quickly. During the detection process I’m grabbing about 10 frames/second.

I don’t understand how the same image is getting written to different files after a 15 second delay. (‘direction’ == NORTHBOUND in the test).


P.S.: I tried posting both pics (they’re identical) but this forum will only let a new user post one media object.

I think I figured it out. I added a cap.release() and then re-established a connection with the camera before taking the pic and that gave me a fresh picture.

yes. never ever sleep while reading from a camera.

the two pictures you get should not be identical but adjacent frames from the camera’s feed, which is probably 30 fps.

you do not need to reopen the camera.

put just the camera reading in its own thread, where the camera is always read, and no reading anywhere else. store the frame in a variable. use that picture whenever you need to. then you can sleep (anywhere but in that thread).

1 Like