app.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import cv2
  2. import numpy as np
  3. import gradio as gr # type: ignore
  4. from mbnet import load_model, detect_objects, get_box_dimensions, draw_labels, load_img
  5. from yolov3 import load_image, load_yolo, detect_objects_yolo, get_box_dimensions_yolo, draw_labels_yolo
  6. # Image Inference
  7. def img_inf(img, model):
  8. if model == "MobileNet-SSD":
  9. model, classes, colors = load_model()
  10. image, height, width, channels = load_img(img)
  11. blob, outputs = detect_objects(image, model)
  12. boxes, class_ids = get_box_dimensions(outputs, height, width)
  13. image1 = draw_labels(boxes, colors, class_ids, classes, image)
  14. return cv2.cvtColor(image1, cv2.COLOR_BGR2RGB)
  15. else:
  16. model, classes, colors, output_layers = load_yolo()
  17. image, height, width, channels = load_image(img)
  18. blob, outputs = detect_objects_yolo(image, model, output_layers)
  19. boxes, confs, class_ids = get_box_dimensions_yolo(outputs, height, width)
  20. image = draw_labels_yolo(boxes, confs, colors, class_ids, classes, image)
  21. return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  22. model_name = gr.Radio(["MobileNet-SSD", "YOLOv3"], value="YOLOv3", label="Model", info="choose your model")
  23. inputs_image = gr.Image(type="filepath", label="Input Image")
  24. outputs_image = gr.Image(type="numpy", label="Output Image")
  25. interface_image = gr.Interface(
  26. fn=img_inf,
  27. inputs=[inputs_image, model_name],
  28. outputs=outputs_image,
  29. title="Image Inference",
  30. description="Upload your photo and select one model and see the results!",
  31. examples=[["sample/dog.jpg"]],
  32. cache_examples=False,
  33. )
  34. # Video Inference
  35. def vid_inf(vid, model_type):
  36. if model_type == "MobileNet-SSD":
  37. cap = cv2.VideoCapture(vid)
  38. # get the video frames' width and height for proper saving of videos
  39. frame_width = int(cap.get(3))
  40. frame_height = int(cap.get(4))
  41. fps = int(cap.get(cv2.CAP_PROP_FPS))
  42. frame_size = (frame_width, frame_height)
  43. fourcc = cv2.VideoWriter_fourcc(*"mp4v")
  44. output_video = "output_recorded.mp4"
  45. # create the `VideoWriter()` object
  46. out = cv2.VideoWriter(output_video, fourcc, fps, frame_size)
  47. model, classes, colors = load_model()
  48. while cap.isOpened():
  49. ret, frame = cap.read()
  50. if ret:
  51. height, width, channels = frame.shape
  52. blob, outputs = detect_objects(frame, model)
  53. boxes, class_ids = get_box_dimensions(outputs, height, width)
  54. frame = draw_labels(boxes, colors, class_ids, classes, frame)
  55. out.write(frame)
  56. yield cv2.cvtColor(frame, cv2.COLOR_BGR2RGB), None
  57. else:
  58. break
  59. cap.release()
  60. out.release()
  61. cv2.destroyAllWindows()
  62. yield None, output_video
  63. else:
  64. cap = cv2.VideoCapture(vid)
  65. # get the video frames' width and height for proper saving of videos
  66. frame_width = int(cap.get(3))
  67. frame_height = int(cap.get(4))
  68. fps = int(cap.get(cv2.CAP_PROP_FPS))
  69. frame_size = (frame_width, frame_height)
  70. fourcc = cv2.VideoWriter_fourcc(*"mp4v")
  71. output_video = "output_recorded.mp4"
  72. # create the `VideoWriter()` object
  73. out = cv2.VideoWriter(output_video, fourcc, fps, frame_size)
  74. model, classes, colors, output_layers = load_yolo()
  75. while cap.isOpened():
  76. ret, frame_y = cap.read()
  77. if ret:
  78. height, width, channels = frame_y.shape
  79. blob, outputs = detect_objects_yolo(frame_y, model, output_layers)
  80. boxes, confs, class_ids = get_box_dimensions_yolo(outputs, height, width)
  81. frame_y = draw_labels_yolo(boxes, confs, colors, class_ids, classes, frame_y)
  82. out.write(frame_y)
  83. yield cv2.cvtColor(frame_y, cv2.COLOR_BGR2RGB), None
  84. else:
  85. break
  86. cap.release()
  87. out.release()
  88. cv2.destroyAllWindows()
  89. yield None, output_video
  90. model_name = gr.Radio(["MobileNet-SSD", "YOLOv3"], value="YOLOv3", label="Model", info="choose your model")
  91. input_video = gr.Video(sources=None, label="Input Video")
  92. output_frame = gr.Image(type="numpy", label="Output Frames")
  93. output_video_file = gr.Video(label="Output video")
  94. interface_video = gr.Interface(
  95. fn=vid_inf,
  96. inputs=[input_video, model_name],
  97. outputs=[output_frame, output_video_file],
  98. title="Video Inference",
  99. description="Upload your video and select one model and see the results!",
  100. examples=[["sample/video_1.mp4"], ["sample/person.mp4"]],
  101. cache_examples=False,
  102. )
  103. gr.TabbedInterface(
  104. [interface_image, interface_video], tab_names=["Image", "Video"], title="GradioxOpenCV-DNN"
  105. ).queue().launch()