Commit fee8eff1 authored by Pramodya Hettiarachchi's avatar Pramodya Hettiarachchi

Merge branch 'it18255720_jayakody' into 'master'

Update microwave detection

See merge request !27
parents a9b1fe37 1103de27
......@@ -5,27 +5,30 @@ from imageai.Detection import ObjectDetection, VideoObjectDetection
import sys
import os
import cv2
import time
from shapely.geometry import box
THRESHOLD_ITEM = 0.5
THRESHOLD_PERSON = 0.5
print("Sharp Object Detection Begins")
execution_path = "input/"
Output_Path = "output/"
#print("Sharp Object Detection Begins")
detector = VideoObjectDetection()
detector2 = VideoObjectDetection()
detector = VideoObjectDetection()
detector2 = VideoObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath("../model/yolo.h5")
detector.CustomObjects(microwave=True,oven=True,person=True)
detector.setModelPath("models/yolo.h5")
detector.loadModel(detection_speed="fast")
detector.loadModel()
detector2.setModelTypeAsRetinaNet()
detector2.setModelPath("model/resnet50_coco_best_v2.1.0.h5")
detector2.loadModel()
def forFrame(frame_number, output_array, output_count, returned_frame):
microwave_count = 0
......@@ -39,7 +42,7 @@ def forFrame(frame_number, output_array, output_count, returned_frame):
count = 0
persons = []
# YOLO Model (Person) and RetinaModel (Toaster, Microwave and Oven, Hair Dryer) both - Single Frame - Person
for detection in output_array:
print(
detection['name'] + " - " + str(detection['percentage_probability']) + " - " + str(detection['box_points']))
......@@ -71,7 +74,8 @@ def forFrame(frame_number, output_array, output_count, returned_frame):
powercords.append(detection['box_points'])
print(detection["name"], " : ", detection["percentage_probability"], " : ", detection["box_points"])
# Check if Child is in 100 Pixels to Left and 100 Pixels to Right of the Identified Object(s)
# Check if there is a person in view
if (len(persons) == 0):
print("There is no Person in the Room!")
......@@ -80,33 +84,38 @@ def forFrame(frame_number, output_array, output_count, returned_frame):
if (len(persons) >= 1):
for person in persons:
# Create Person(minx, miny, maxx, maxy)
person_box = box(person[0], person[3], person[1], person[2])
coords = person
coords = person # (x1, x2, y1, y2)
x1 = person[0]
x2 = person[1]
y1 = person[2]
y2 = person[3]
person_midpoint = ((x1 + x2) / 2, (y1 + y2) / 2)
x_center = (x1 + x2) / 2
y_center = (y1 + y2) / 2
x_center = (x1 + x2) / 2 # Center X Coordinate
y_center = (y1 + y2) / 2 # Center Y Coordinate
# x1 => Left, x2 => Right, y1 => Top, y2 => Bottom
# (x1, y1) - Top Left
# (x2, y2) - Bottom Right
# (x2, y1) - Top Right
# (x1, y2) - Bottom Left
if microwave_count > 0:
# Iterate each Microwave
for microwave in microwaves:
# Create Microwave(minx, miny, maxx, maxy)
microwave_box = box(microwave[0] + 50, microwave[3] + 50, microwave[1] + 50, microwave[2] + 50)
e_x1 = microwave[0] + 50
e_x2 = microwave[1] + 50
e_y1 = microwave[2] + 50
e_y2 = microwave[3] + 50
# Check if Microwave Overlaps with the Person's Safety Boundary
# We Add 50px in all directions of the Bounding Box - Top+50, Bottom+50, Left+50, Right+50
e_x1 = microwave[0] + 50 # Left
e_x2 = microwave[1] + 50 # Right
e_y1 = microwave[2] + 50 # Top
e_y2 = microwave[3] + 50 # Bottom
overlap = False
# Check if the Microwave Oven is in the Persons Danger Region
if (microwave_box.intersection(person_box) != None):
print("Microwave/Oven - Person is in the Microwave/Oven's Danger Region!")
overlap = True
......@@ -125,7 +134,8 @@ def forFrame(frame_number, output_array, output_count, returned_frame):
overlap = True
if (overlap == True):
# If the Bounding Boxes Overlap at some point
# Calculate Distnace between center point of two boxes
microwave_midpoint = ((e_x1 + e_x2) / 2, (e_y1 + e_y2) / 2)
distance = ((((microwave_midpoint[0] - person_midpoint[0]) ** 2) + (
(microwave_midpoint[1] - person_midpoint[1]) ** 2)) ** 0.5)
......@@ -196,7 +206,7 @@ def forFrame(frame_number, output_array, output_count, returned_frame):
powercord_thresh = cv2.dilate(powercord_thresh, None, iterations=4)
# detections = detector.detectObjectsFromImage(input_image="input/electric1.jpg", output_image_path="output/electric1.jpg")
detector.detectObjectsFromVideo(input_file_path=os.path.join("output/video1.mp4", "video1.mp4"),output_file_path=os.path.join("output/video1.mp4", "video_frame_analysis"),frames_per_second=24, per_frame_function=forFrame,minimum_percentage_probability=50, return_detected_frame=True)
detector.detectObjectsFromVideo(input_file_path=os.path.join(execution_path, "oven1.mp4"), output_file_path=os.path.join(Output_Path, "video_frame_analysis_oven1.mp4") , frames_per_second=24, per_frame_function=forFrame, minimum_percentage_probability=50, return_detected_frame=True)
# detections2 = detector2.detectObjectsFromImage(input_image="input/sharp2.jpg", output_image_path="output/sharp2.jpg")
detector2.detectObjectsFromVideo(input_file_path=os.path.join("output/video1.mp4", "video1.mp4"),output_file_path=os.path.join("output/video1.mp4", "video_frame_analysis"),frames_per_second=24, per_frame_function=forFrame,minimum_percentage_probability=50, return_detected_frame=True)
\ No newline at end of file
# detector2.detectObjectsFromVideo(input_file_path=os.path.join(execution_path, "oven1.mp4"), output_file_path=os.path.join(execution_path, "video_frame_analysis_oven1.mp4") , frames_per_second=24, per_frame_function=forFrame, minimum_percentage_probability=50, return_detected_frame=True)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment