How to do prediction in video
Hi, i saw how to do detection in an image using model_inspect.py. But how to perform this detection in video or real-time camera input(i mean frame-by-frame)?
Hi @PiyalGeorge, I just added a new utility here:
https://github.com/google/automl/blob/7768c499ede4a732fde022a24d8525b3396b50a4/efficientdet/inference.py#L240
So you can do something like this:
driver = inference.ServingDriver('efficientdet-d0', '/tmp/efficientdet-d0')
driver.build()
for f in get_frame_from_video():
image = Image.open(f)
predictions = driver.serve(image)
out_image = driver.visualize(image, predictions[0])
This is just a demo, all input preprocessing and output postprocessing run with single thread, so it is probably not efficient. Hopefully we can optimize the efficiency in future.
I am going to close this issue for now. Feel free to reopen it if it doesn't work for you or you want to contribute the efficiency optimization. Thanks!
I tried this way.
from PIL import Image import cv2 from imutils.video import FPS
def inference_single_image(self, image_image_path, output_dir, **kwargs):
driver = inference.ServingDriver(self.model_name, self.ckpt_path, self.image_size)
driver.build()
cap = cv2.VideoCapture(image_image_path)
fps = FPS().start()
while True:
res, img = cap.read()
if img is None:
break
image = Image.fromarray(img)
predictions = driver.serve(image)
out_image = driver.visualize(image, predictions[0])
fps.update()
img = np.array(out_image)
cv2.imshow("Detection", img)
k = cv2.waitKey(1)
if k == 27:
break
fps.stop()
print("[INFO] elapsed time: {:.2f}".format(fps.elapsed()))
print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
Hi,@WonTaeYeon thanks for your contribution in detecting the frame of video, I changed your code, because the score threshold to show box need to be set, and the windows size of showing predicted result needed bo be set proper size, if the size of raw image is very big, the changed code is as follows:
def inference_single_image(self, image_image_path, output_dir, **kwargs):
driver = inference.ServingDriver(self.model_name, self.ckpt_path, self.image_size)
driver.build()
cap = cv2.VideoCapture(image_image_path)
fps = FPS().start()
while True:
res, img = cap.read()
if img is None:
break
image = Image.fromarray(img)
predictions = driver.serve(image)
out_image = driver.visualize(image, predictions[0], **kwargs)
resize_img = cv2.resize(out_image, (1920, 1080))
fps.update()
img = np.array(resize_img)
cv2.imshow("Detection", img)
k = cv2.waitKey(1)
if k == 27:
break
fps.stop()
print("[INFO] elapsed time: {:.2f}".format(fps.elapsed()))
print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
def inference_single_image(self, image_image_path, output_dir, **kwargs): driver = inference.ServingDriver(self.model_name, self.ckpt_path, self.image_size) driver.build() cap = cv2.VideoCapture(image_image_path) fps = FPS().start() while True: res, img = cap.read() if img is None: break image = Image.fromarray(img) predictions = driver.serve(image) out_image = driver.visualize(image, predictions[0], **kwargs) resize_img = cv2.resize(out_image, (1920, 1080)) fps.update() img = np.array(resize_img) cv2.imshow("Detection", img) k = cv2.waitKey(1) if k == 27: break fps.stop() print("[INFO] elapsed time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
Hi @MrCuiHao , I have this error
File "inference_video.py", line 40, in <module>
inference_single_image(image_image_path= "/home/sistemasinteligentesmaps/Downloads/DJI_0001.MP4",output_dir="/home/sistemasinteligentesmaps/Downloads")
File "inference_video.py", line 26, in inference_single_image
predictions = driver.serve(image)
AttributeError: 'ServingDriver' object has no attribute 'serve'
this is my code:
`#https://github.com/google/automl/issues/43
import inference import cv2 from PIL import Image from imutils.video import FPS
model_name = 'efficientdet-d0' ckpt_path = '/home/sistemasinteligentesmaps/automl/efficientdet/efficientdet-d0' image_size = 1920
import tensorflow.compat.v1 as tf
def inference_single_image(image_image_path, output_dir, **kwargs): tf.reset_default_graph() driver = inference.ServingDriver(model_name, ckpt_path,image_size=image_size) driver.build() cap = cv2.VideoCapture(image_image_path) fps = FPS().start() while True: res, img = cap.read() if img is None: break image = Image.fromarray(img) predictions = driver.serve(image) out_image = driver.visualize(image, predictions[0], **kwargs) resize_img = cv2.resize(out_image, (1920, 1080)) fps.update() img = np.array(resize_img) cv2.imshow("Detection", img) k = cv2.waitKey(1) if k == 27: break fps.stop() print("[INFO] elapsed time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
inference_single_image(image_image_path= "/home/Downloads/DJI_0001.MP4",output_dir="/home/Downloads")`