Save detected image to disk


I want to alter so that a picture (including the frame) of a detected object is saved to disk.
At the moment, this part of the code captures the image from the camera and checks if a keyboard (for testing) is detected.
It then sends me a message via telegram:

while True:
	# capture the next image
	img = input.Capture()

	# detect objects in the image (with overlay)
	detections = net.Detect(img, overlay=opt.overlay)

	# print the detections
	print("detected {:d} objects in image".format(len(detections)))

	for detection in detections:

	# render the image

	# update the title bar
	output.SetStatus("{:s} | Network {:.0f} FPS".format(, net.GetNetworkFPS()))

    for detection in detections:
        if detection.ClassID == 76:
	        i = i + 1
            if i > 30:
                bot.send_video(chat_id="", photo=open('/tmp/output.mp4', 'rb'), caption="mycaption")
                i = 0

	# exit on input/output EOS
	if not input.IsStreaming() or not output.IsStreaming():

The ultimate goal is to send me an image of the detected object via telegram, but I can’t get my head around how to render just the last frame and send it as a jpeg.
If it is easier, a video would also be okay.

Can anybody point me in the right direction?
Thanks :)


Suppose you should be able to save the img data into a jpeg file directly with cv2.imwrite().


thanks for your answer.
I looked into it and while playing around, I noticed that my solution was already pretty close.
It works now as expected :)