Skip to content

Instantly share code, notes, and snippets.

@OlafenwaMoses
Last active April 10, 2022 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OlafenwaMoses/a943de4a9c4125d9b2580061b726a6fa to your computer and use it in GitHub Desktop.
Save OlafenwaMoses/a943de4a9c4125d9b2580061b726a6fa to your computer and use it in GitHub Desktop.
from imageai.Detection import VideoObjectDetection
import os
import cv2
from matplotlib import pyplot as plt
import matplotlib
# The line below prints the Matplotlib version to the console first.
print(matplotlib.__version__)
execution_path = os.getcwd()
camera = cv2.VideoCapture(0)
frame_number = 0
color_index = {'bus': 'red', 'handbag': 'steelblue', 'giraffe': 'orange', 'spoon': 'gray', 'cup': 'yellow', 'chair': 'green', 'elephant': 'pink', 'truck': 'indigo', 'motorcycle': 'azure', 'refrigerator': 'gold', 'keyboard': 'violet', 'cow': 'magenta', 'mouse': 'crimson', 'sports ball': 'raspberry', 'horse': 'maroon', 'cat': 'orchid', 'boat': 'slateblue', 'hot dog': 'navy', 'apple': 'cobalt', 'parking meter': 'aliceblue', 'sandwich': 'skyblue', 'skis': 'deepskyblue', 'microwave': 'peacock', 'knife': 'cadetblue', 'baseball bat': 'cyan', 'oven': 'lightcyan', 'carrot': 'coldgrey', 'scissors': 'seagreen', 'sheep': 'deepgreen', 'toothbrush': 'cobaltgreen', 'fire hydrant': 'limegreen', 'remote': 'forestgreen', 'bicycle': 'olivedrab', 'toilet': 'ivory', 'tv': 'khaki', 'skateboard': 'palegoldenrod', 'train': 'cornsilk', 'zebra': 'wheat', 'tie': 'burlywood', 'orange': 'melon', 'bird': 'bisque', 'dining table': 'chocolate', 'hair drier': 'sandybrown', 'cell phone': 'sienna', 'sink': 'coral', 'bench': 'salmon', 'bottle': 'brown', 'car': 'silver', 'bowl': 'maroon', 'tennis racket': 'palevilotered', 'airplane': 'lavenderblush', 'pizza': 'hotpink', 'umbrella': 'deeppink', 'bear': 'plum', 'fork': 'purple', 'laptop': 'indigo', 'vase': 'mediumpurple', 'baseball glove': 'slateblue', 'traffic light': 'mediumblue', 'bed': 'navy', 'broccoli': 'royalblue', 'backpack': 'slategray', 'snowboard': 'skyblue', 'kite': 'cadetblue', 'teddy bear': 'peacock', 'clock': 'lightcyan', 'wine glass': 'teal', 'frisbee': 'aquamarine', 'donut': 'mincream', 'suitcase': 'seagreen', 'dog': 'springgreen', 'banana': 'emeraldgreen', 'person': 'honeydew', 'surfboard': 'palegreen', 'cake': 'sapgreen', 'book': 'lawngreen', 'potted plant': 'greenyellow', 'toaster': 'ivory', 'stop sign': 'beige', 'couch': 'khaki'}
resized = False
def forSecond(frame2_number, output_arrays, count_arrays, average_count, returned_frame):
global frame_number
frame_number += 1
plt.clf()
this_colors = []
labels = []
sizes = []
counter = 0
for eachItem in average_count:
counter += 1
labels.append(eachItem + " = " + str(average_count[eachItem]))
sizes.append(average_count[eachItem])
this_colors.append(color_index[eachItem])
global resized
if (resized == False):
manager = plt.get_current_fig_manager()
# The line below has been adjusted with the 'width' and 'height' tags removed
manager.resize(1000,500)
resized = True
plt.subplot(1, 2, 1)
plt.title("Second : " + str(frame_number))
plt.axis("off")
plt.imshow(returned_frame, interpolation="none")
plt.subplot(1, 2, 2)
plt.title("Analysis: " + str(frame_number))
plt.pie(sizes, labels=labels, colors=this_colors, shadow=True, startangle=140, autopct="%1.1f%%")
plt.pause(0.01)
video_detector = VideoObjectDetection()
video_detector.setModelTypeAsYOLOv3()
video_detector.setModelPath(os.path.join(execution_path, "yolo.h5"))
video_detector.loadModel()
plt.show()
# To enforce that only one frame is processed in a second, the parameter 'frame_detection_interval=20' has been set.
# In simple terms, the number of frames to be processed in a second is 'frames_per_second / frame_detection_interval' . The default value for ' frame_detection_interval' is 1.
video_detector.detectObjectsFromVideo(camera_input=camera, output_file_path=os.path.join(execution_path, "video_second_analysis") , frames_per_second=20, per_second_function=forSecond, minimum_percentage_probability=50, return_detected_frame=True, log_progress=True, frame_detection_interval=20)
@hiraayaa
Copy link

hiraayaa commented Apr 10, 2022

Hi, I'm a newbie. I have a problem with how the color changes on the returned frame. My skin appears to be blue on the frame, is there any way to fix it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment