def Crop_video(self, params): global input_video if 'videoName' in params.keys(): input_video = str(params['videoName']) print(input_video) else: return "Error: No Video Name field provided. Please specify an url." detector = VideoObjectDetection() detector.setModelTypeAsYOLOv3() detector.setModelPath( os.path.join(parent, 'Mobilaty\\project\\public\\yolo.h5')) detector.loadModel() custom_objects = detector.CustomObjects(cell_phone=True) video_path = detector.detectCustomObjectsFromVideo( custom_objects=custom_objects, input_file_path=os.path.join(Base_Video_path, input_video), output_file_path=os.path.join(Base_Video_path, "traffic_custom_detected"), save_detected_video=False, frames_per_second=1, per_frame_function=forFrame) os.remove(os.path.join(Base_Video_path, "traffic_custom_detected")) return "Done!"
def objectDection(execution_path, save_path, fileName): ''' detecting object for each frame ''' detector = VideoObjectDetection() detector.setModelTypeAsYOLOv3() detector.setModelPath(os.path.join(execution_path, "yolo.h5")) detector.loadModel() global FILENAME FILENAME = fileName.split('.')[0] custom_objects = detector.CustomObjects(car=True, truck=True, bus=True) video_path = detector.detectCustomObjectsFromVideo( custom_objects=custom_objects, input_file_path=os.path.join(execution_path, fileName), output_file_path=os.path.join(save_path, fileName.split(".")[0] + "_detected"), frames_per_second=30, frame_detection_interval=1, per_frame_function=forFrame, # per_second_function= forSecond, minimum_percentage_probability=79, # return_detected_frame=True, log_progress=True) return (fileName.split(".")[0] + "_detected.avi")
def detection_of_vehicles_from_video(folder1, folder2, findex): ''' Detects and saves the arrays containing bounding boxes of detected vehicles from videos of a given folder Parameters: folder1 : path of the folder containing videos folder2 : path of the folder in which arrays are required to be stored findex : index number of the first video in folder1 ''' #modifying forFrame function of ImageAI to make a list #of bounding box coordinates for vehichles detected in a #particular frame def forFrame(frame_number, output_array, output_count): bboxes = [] for i in range(len(output_array)): bboxes.append(list(output_array[i]['box_points'])) B.append(bboxes) #reading and sorting the filenames of folder1 videos = glob.glob(folder1 + '/video*.MOV') videos = natsort.natsorted(videos) #set and load ResNet Model for detection of vehicles execution_path = os.getcwd() detector = VideoObjectDetection() detector.setModelTypeAsRetinaNet() #use detector.setModelTypeAsYOLOv3() to use YOLOv3 instead of RetinaNet detector.setModelPath( os.path.join( execution_path, "/home/siddhi/Desktop/RoadCrossingAssistant_FY_Project_Data/resnet50_coco_best_v2.0.1.h5" )) #use model path of yolo.h5 if to use YOLOv3 instead of RetinaNet detector.loadModel() custom_objects = detector.CustomObjects(bicycle=True, motorcycle=True, car=True, truck=True) for video in videos: print('processing' + video) B = [] detector.detectCustomObjectsFromVideo( save_detected_video=False, custom_objects=custom_objects, input_file_path=os.path.join(execution_path, video), frames_per_second=30, per_frame_function=forFrame, minimum_percentage_probability=40) B = np.array(B) print('saving array for video' + video + '\n shape of array: ' + str(B.shape)) np.save(folder2 + '/array' + str(findex), B) findex = findex + 1
def annotate_humans( input_file_path: Path, minimum_percentage_probability: int = 60, model_filename: str = "resnet50_coco_best_v2.1.0.h5", ): """ Make use of imageai.Detection to annotate an input video with bounding boxes indicating the location of detected persons. Annotated video saved in /output folder. Args: input_file_path (Path): Path of input video to annotate minimum_percentage_probability (int, optional): Detection threshold probability (in %), model specific. Defaults to 60. model_filename (str, optional): Name of pretrained model stored in /data folder. Defaults to "resnet50_coco_best_v2.1.0.h5". """ # indicate detection parameters in the output file name output_file_name = f"{input_file_path.stem}_{minimum_percentage_probability}%_{model_filename.split('.')[0]}" output_file_path = Path("output") / (output_file_name + ".avi") detector = VideoObjectDetection() detector.setModelTypeAsRetinaNet() # change this method if using alternate models (e.g. YOLO) detector.setModelPath(Path("data") / model_filename) detector.loadModel() input_file_path = str(input_file_path.absolute()) # str required by imageai # perform detection on each frame of the video video_path = detector.detectObjectsFromVideo( input_file_path=str(input_file_path), custom_objects=detector.CustomObjects( person=True # we're only interested in detecting humans ), output_file_path=str(Path("output") / output_file_name), frames_per_second=25, # same fps as input video frame_detection_interval=1, log_progress=True, minimum_percentage_probability=minimum_percentage_probability, )
video_detector = VideoObjectDetection() video_detector.setModelTypeAsYOLOv3() video_detector.setModelPath(os.path.join(execution_path, "yolo.h5")) video_detector.loadModel() #plt.show() custom = video_detector.CustomObjects(person=True, bicycle=True, car=True, motorcycle=True, airplane=True, bus=True, train=True, truck=True, boat=True, traffic_light=True, fire_hydrant=True, stop_sign=True, parking_meter=True, bench=True, bird=True, cat=True, dog=True, horse=True, sheep=True, cow=True) video_detector.detectObjectsFromVideo(camera_input=cv2.VideoCapture(0), save_detected_video=False, frames_per_second=20, per_frame_function=forFrame, minimum_percentage_probability=30, return_detected_frame=True)
if __name__ == '__main__': execution_path = os.getcwd() detector = VideoObjectDetection() detector.setModelTypeAsYOLOv3() detector.setModelPath(os.path.join(execution_path, "yolo.h5")) detector.loadModel(detection_speed="faster") custom_objects = detector.CustomObjects(car=True, person=True, bus=True, chair=True, truck=True, refrigerator=True, oven=True, bicycle=True, skateboard=True, train=True, bench=True, motorcycle=True, bed=True, suitcase=True) detections = detector.detectCustomObjectsFromVideo( camera_input=camera, custom_objects=custom_objects, save_detected_video=False, return_detected_frame=True, per_frame_function=forFrame, minimum_percentage_probability=30, frames_per_second=20,
def forMinute(minute_number, output_arrays, count_arrays, average_output_count): print("MINUTE : ", minute_number) print("Array for the outputs of each frame ", output_arrays) print("Array for output count for unique objects in each frame : ", count_arrays) print("Output average count for unique objects in the last minute: ", average_output_count) print("------------END OF A MINUTE --------------") video_detector = VideoObjectDetection() video_detector.setModelTypeAsYOLOv3() video_detector.setModelPath(os.path.join(execution_path, "yolo.h5")) video_detector.loadModel(detection_speed="faster") custom_objects = video_detector.CustomObjects(person=True) video_path = video_detector.detectCustomObjectsFromVideo( custom_objects=custom_objects, input_file_path=os.path.join(execution_path, "test_customObject.mp4"), output_file_path=os.path.join(execution_path, "testcustom"), frames_per_second=30, log_progress=True) print(video_path) e = time.time() timetaken = e - s print(timetaken)
print("Number from license plate is : - ") license_number = reading_licenseplate(img) # arr[x] = license_number except Exception as err: print("Count Detect the number plate ", err) print(license_number) # cam = cv2.VideoCapture(0) something = None execution_path = os.getcwd() detector = VideoObjectDetection() detector.setModelTypeAsYOLOv3() detector.setModelPath(os.path.join(execution_path , "yolo.h5")) custom = detector.CustomObjects(car=True) detector.loadModel(detection_speed='normal') def forFrame(frame_number, output_array, output_count,returned_frame): if output_array != []: # print("FOR FRAME " , frame_number) # print("Output for each object : ", output_array) # print("Output count for unique objects : ", output_count) print("test") cv2.line(returned_frame,(0,550),(1700,550),color=cv2.COLOR_BAYER_BG2RGB_VNG,thickness=4) #cv2.imshow("Any",returned_frame) print((output_array[0]['box_points'][3]+output_array[0]['box_points'][1])/2) if (output_array[0]['box_points'][3]+output_array[0]['box_points'][1])/2 in range(330,360): cv2.line(returned_frame, (0, 550), (1300, 550), color=cv2.COLOR_BAYER_GB2RGB_EA, thickness=4) cv2.imshow("line_img",returned_frame)
def forSeconds(second_number, output_arrays, count_arrays, average_output_count): print("SECOND : ", second_number) print("Array for the outputs of each frame ", output_arrays) print("Array for output count for unique objects in each frame : ", count_arrays) print("Output average count for unique objects in the last second: ", average_output_count) print("------------END OF A SECOND --------------") #-------------------------------------------------------------------------------# detector = VideoObjectDetection() detector.setModelTypeAsYOLOv3() detector.setModelPath(os.path.join(execution_path,"yolo.h5")) detector.loadModel(detection_speed="fastest") plt.show() custom_objects = detector.CustomObjects(car=True,truck=True,motorcycle=True) #--------------------------------Features---------------------------------------# video_path = detector.detectCustomObjectsFromVideo( minimum_percentage_probability=50, custom_objects=custom_objects, per_second_function=forSeconds, display_percentage_probability=True, display_object_name=True, log_progress=True, frames_per_second=60, #--------------------Input-File--------------------------------------------# input_file_path=os.path.join(execution_path,"cars.mp4"), #--------------------Output-File-----------------------------------------# output_file_path=os.path.join(execution_path, "car_detection")) #---------------------Finish------------------------------------------#
import cv2 # Instantiate detector and activate webcam detector = VideoObjectDetection() video_capture = cv2.VideoCapture(0) # Set and load model model_path = "models/yolo.h5" detector.setModelTypeAsYOLOv3() detector.setModelPath(model_path) detector.loadModel() # Set custom classes custom_objects = detector.CustomObjects(car=True, motorcycle=True, person=True, bicycle=True, truck=True, dog=True) # Functions to parse into the detection for every frame and execute which takes analytical data of the frames. def forFrame(frame_number, output_array, output_count): print("FOR FRAME ", frame_number) print("Output for each object : ", output_array) print("Output count for unique objects : ", output_count) print("------------END OF A FRAME --------------") def forSeconds(second_number, output_arrays, count_arrays, average_output_count): print("SECOND : ", second_number)
class VideoObjectDetector: def __init__(self, exec_path): self.exec_path = exec_path self.detector = VideoObjectDetection() self.config = ConfigLoader().conf self.detector.setModelTypeAsRetinaNet() self.detector.setModelPath( os.path.join(self.exec_path, self.config['obj-detector.model-path'])) self.detector.loadModel() def for_frame(self, frame_number, output_array, output_count, returned_frame, resized=False): 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' } plt.clf() this_colors = [] labels = [] sizes = [] counter = 0 for eachItem in output_count: counter += 1 labels.append(eachItem + " = " + str(output_count[eachItem])) sizes.append(output_count[eachItem]) this_colors.append(color_index[eachItem]) resized if not resized: manager = plt.get_current_fig_manager() manager.resize(width=1000, height=500) resized = True plt.subplot(1, 2, 1) plt.title("Frame : " + 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) def run_inference_on(self, camera): self.detector.detectCustomObjectsFromVideo( custom_objects=self.detector.CustomObjects(person=True), camera_input=camera, frames_per_second=2, log_progress=True, save_detected_video=False, per_frame_function=self.for_frame, return_detected_frame=True)
import vibrations execution_path = os.getcwd() # Get ImageAI video detection module detector = VideoObjectDetection() # set objects to detect custom_objects = detector.CustomObjects(person=True, bottle=True, wine_glass=True, cup=True, fork=True, knife=True, spoon=True, bowl=True, banana=True, apple=True, sandwich=True, orange=True, broccoli=True, carrot=True, hot_dog=True, pizza=True, donut=True, cake=True, cell_phone=True) # Set model type detector.setModelTypeAsYOLOv3() # Uncomment below as required for RetinaNet, set type and path # detector.setModelTypeAsRetinaNet() detector.setModelPath(os.path.join(execution_path, "yolo.h5")) # detector.setModelPath(os.path.join(execution_path, "resnet_coco_best_v2.0.1.h5"))
count = [] def forFrame(frame_number, output_array, output_count): if output_array: f_number.append(frame_number) output.append(output_array) count.append(output_count) print("FOR FRAME", frame_number) print("Output for each object : ", output_array) print("Output count for unique objects : ", output_count) print("------------END OF A FRAME --------------") return frame_number, output_array, output_count execution_path = os.getcwd() detector = VideoObjectDetection() detector.setModelTypeAsYOLOv3() detector.setModelPath(os.path.join(execution_path, "/models/yolo.h5")) detector.loadModel() detector.CustomObjects(fish=True) detector.detectObjectsFromVideo( input_file_path=os.path.join(execution_path, "datasets/city.mp4"), output_file_path=os.path.join(execution_path, "city_result"), frames_per_second=29, log_progress=True, per_frame_function=forFrame, minimum_percentage_probability=60)
## print("acceleration",acceleration[i]) i += 1 # #============================================================================== # location[i]=data["box_points"] # for i in range(len(output_array)): # print(location[i]) #============================================================================== execution_path = os.getcwd() detector = VideoObjectDetection() detector.setModelTypeAsRetinaNet() detector.setModelPath( os.path.join(execution_path, 'resnet50_coco_best_v2.0.1.h5')) detector.loadModel(detection_speed="fast") custom_objects = detector.CustomObjects(car=True) video_path = detector.detectCustomObjectsFromVideo( custom_objects=custom_objects, input_file_path=os.path.join(execution_path, 'traffic.mp4'), output_file_path=os.path.join(execution_path, 'traffic-detected'), frames_per_second=20, #============================================================================== # frame_detection_interval=1, #============================================================================== per_frame_function=forFrame, minimum_percentage_probability=30)
from imageai.Detection import VideoObjectDetection import os execution_path = os.getcwd() detector = VideoObjectDetection() detector.setModelTypeAsRetinaNet() detector.setModelPath( os.path.join(execution_path , "../models/resnet50_coco_best_v2.0.1.h5")) detector.loadModel() custom = detector.CustomObjects(person=True, motorcycle=True, bus=True) video_path = detector.detectCustomObjectsFromVideo(custom_objects=custom, input_file_path=os.path.join(execution_path, "traffic-mini.mp4"), output_file_path=os.path.join(execution_path, "traffic-mini_detected_custom") , frames_per_second=20, log_progress=True) print(video_path)
from imageai.Detection import VideoObjectDetection import os execution_path = os.getcwd() detector = VideoObjectDetection() detector.setModelTypeAsRetinaNet() detector.setModelPath( os.path.join(execution_path , "resnet50_coco_best_v2.1.0.h5")) detector.loadModel() custom_objects = detector.CustomObjects(person=True, bicycle=True, motorcycle=True) video_path = detector.detectObjectsFromVideo( input_file_path=os.path.join(execution_path, "Animal.mp4"), output_file_path=os.path.join(execution_path, "Animal_output"), frames_per_second=1, log_progress=True) print(video_path)
import os boxes = [] def forFrame(frame_number, output_array, output_count): boxes.append(output_array) print("FOR FRAME ", frame_number) print("Output for each object : ", output_array) print("Output count for unique objects : ", output_count) print("------------END OF A FRAME --------------") execution_path = os.getcwd() detector = VideoObjectDetection() detector.setModelTypeAsRetinaNet() detector.setModelPath( os.path.join(execution_path, "checkpoints/resnet50_coco_best_v2.1.0.h5")) detector.loadModel() custom_objects = detector.CustomObjects(sports_ball=True, baseball_bat=True) video_path = detector.detectCustomObjectsFromVideo( custom_objects=custom_objects, input_file_path=os.path.join(execution_path, "data/red_cricket.avi"), output_file_path=os.path.join(execution_path, "results/bat_ball_det"), frames_per_second=2, log_progress=True, per_frame_function=forFrame) print(video_path)
from imageai.Detection import VideoObjectDetection import os, logging logging.basicConfig(level=logging.DEBUG, filename='app.log', filemode='w', format='%(name)s - %(levelname)s - %(message)s') execution_path = os.getcwd() detector = VideoObjectDetection() detector.setModelTypeAsTinyYOLOv3() detector.setModelPath(execution_path + "\\yolo-tiny.h5") detector.loadModel() custom_objects = detector.CustomObjects(person=True) total_seconds = 0 object_visible = 0 frame_count = 0 def forFrame(frame_number, output_array, output_count): global frame_count if "person" in output_count: frame_count += 1 logging.info("Frames with visible objects: %s", frame_count) else: logging.info("No objects visible")
pub_str = rospy.Publisher('objects_detected', String, queue_size=100) custom = video_detector_resnet.CustomObjects(person=True, handbag=True, tie=True, suitcase=True, bottle=True, wine_glass=True, cup=True, fork=True, knife=True, spoon=True, bowl=True, banana=True, apple=True, sandwich=True, orange=True, pizza=True, donut=True, cake=True, chair=True, potted_plant=True, laptop=True, mouse=True, remote=True, keyboard=True, cell_phone=True, book=True, clock=True, scissors=True)