def test_video_detection_retinanet_analysis(): try: keras.backend.clear_session() except: None detector = VideoObjectDetection() detector.setModelTypeAsRetinaNet() detector.setModelPath(model_path=os.path.join( main_folder, "data-models", "resnet50_coco_best_v2.0.1.h5")) detector.loadModel(detection_speed="fastest") video_path = detector.detectObjectsFromVideo( input_file_path=video_file, output_file_path=video_file_output, save_detected_video=True, frames_per_second=30, log_progress=True, per_frame_function=forFrame, per_second_function=forSecond, return_detected_frame=True) assert os.path.exists(video_file_output + ".avi") assert isinstance(video_path, str) os.remove(video_file_output + ".avi")
def detect_with_imageai_retinanet(): def forFrame(frame_number, output_array, output_count): """Детекция bounding box'ов""" print("ДЛЯ КАДРА " , frame_number) print('Объект:', output_array[0]['name']) print('Вероятность:', output_array[0]['percentage_probability']) print('Bounding box:', output_array[0]['box_points']) print("Уникальных объектов: ", output_count[output_array[0]['name']]) print("------------END OF A FRAME --------------\n\n") execution_path = os.getcwd() camera = cv2.VideoCapture(0) detector = VideoObjectDetection() detector.setModelTypeAsRetinaNet() detector.setModelPath(os.path.join(execution_path, "weights/resnet50_coco_best_v2.0.1.h5")) detector.loadModel() video_model = detector.detectObjectsFromVideo(camera_input=camera, output_file_path=os.path.join(execution_path, "camera_detected_video"), frames_per_second=20, per_frame_function=forFrame, minimum_percentage_probability=40)
def detected(): form = detectForm(request.args) if form.validate(): input_path = form.data['inputpath'] output_path = form.data['outputpath'] videoName = form.data['videoname'] try: detector = VideoObjectDetection() start = time.time() detector.setModelTypeAsRetinaNet() detector.setModelPath( os.path.join(input_path, "resnet50_coco_best_v2.0.1.h5")) detector.loadModel(detection_speed="fastest") video_path = detector.detectObjectsFromVideo( input_file_path=output_path, output_file_path=os.path.join(input_path, "detected" + videoName), frames_per_second=10, log_progress=True) convert(video_path, input_path + "/dectected" + videoName) end = time.time() processtime = end - start return jsonify({ 'video_path': video_path, 'processtime': processtime }) except Exception as e: raise e return jsonify({'error': 'no correct form'})
def detectionModelLoad(model_path, mod="normal"): detector = VideoObjectDetection() detector.setModelTypeAsRetinaNet() detector.setModelPath(model_path) detector.loadModel(detection_speed=mod) return detector
def start(): global vf ifp = entry_file_path.get() ofp = entry_file_path1.get() if vf == 0: try: print("Starting to render video") detector = VideoObjectDetection() detector.setModelTypeAsYOLOv3() detector.setModelPath(os.path.join(exec_path, "yolo.h5")) detector.loadModel() except: wget.download( "https://github.com/OlafenwaMoses/ImageAI/releases/download/1.0/yolo.h5" ) print("Starting to render video") detector = VideoObjectDetection() detector.setModelTypeAsYOLOv3() detector.setModelPath(os.path.join(exec_path, "yolo.h5")) detector.loadModel() list = detector.detectObjectsFromVideo( input_file_path=os.path.join(exec_path, ifp), output_file_path=os.path.join(exec_path, ofp), frames_per_second=20) label = Label(root, text="Succeful!", fg="green") label.pack() elif vf == 1: try: print("Starting to render photo") detector = ObjectDetection() detector.setModelTypeAsRetinaNet() detector.setModelPath( os.path.join(exec_path, "resnet50_coco_best_v2.0.1.h5")) detector.loadModel() except OSError: wget.download( "https://github.com/OlafenwaMoses/ImageAI/releases/download/1.0/resnet50_coco_best_v2.0.1.h5" ) print("Starting to render photo") detector = ObjectDetection() detector.setModelTypeAsRetinaNet() detector.setModelPath( os.path.join(exec_path, "resnet50_coco_best_v2.0.1.h5")) detector.loadModel() list = detector.detectObjectsFromImage( input_image=os.path.join(exec_path, ifp), output_image_path=os.path.join(exec_path, ofp), display_percentage_probability=True, display_object_name=True) label = Label(root, text="Succeful!", fg="green") label.pack() elif vf == 2: image = face_recognition.load_image_file(exec_path + "/" + ifp) face_landmarks_list = face_recognition.face_landmarks(image) pil_image = Image.fromarray(image) d = ImageDraw.Draw(pil_image) for face_landmarks in face_landmarks_list: for facial_feature in face_landmarks.keys(): d.line(face_landmarks[facial_feature], width=5) pil_image.show()
def object_detection(input_file, out_name, model='ResNet'): video_detector = VideoObjectDetection() if model == "ResNet": video_detector.setModelTypeAsRetinaNet() video_detector.setModelPath( os.path.join(execution_path, "pretranined_models/resnet50_coco_best_v2.0.1.h5")) elif model == "Yolo": video_detector.setModelTypeAsYOLOv3() video_detector.setModelPath( os.path.join(execution_path, "pretranined_models/yolo.h5")) else: video_detector.setModelTypeAsTinyYOLOv3() video_detector.setModelPath( os.path.join(execution_path, "pretranined_models/yolo-tiny.h5")) video_detector.loadModel() vi = video_detector.detectObjectsFromVideo( input_file_path=os.path.join(execution_path, input_file), output_file_path=os.path.join(execution_path, out_name), frames_per_second=10, per_second_function=forSeconds, per_frame_function=forFrame, per_minute_function=forMinute, minimum_percentage_probability=30)
def detect(var_name, var_model_path): # ## var_in_path = os.path.join(os.getcwd(), "cache", var_name + "IN.mp4") var_temp_path = os.path.join(os.getcwd(), "cache", var_name + "TEMP") # var_out_path = os.path.join(os.getcwd(), "cache", var_name + "OUT.mp4") # ## Initiate the detector. var_detector = VideoObjectDetection() var_detector.setModelTypeAsRetinaNet() var_detector.setModelPath(var_model_path) var_detector.loadModel(detection_speed="fast") # ## Perform the object detection. var_detector.detectObjectsFromVideo(input_file_path=var_in_path, output_file_path=var_temp_path, frames_per_second=30, log_progress=True, frame_detection_interval=1, minimum_percentage_probability=15) # ## Convert the format of output video to MP4. var_clip = moviepy.VideoFileClip("cache/" + var_name + "TEMP.avi") var_clip.write_videofile("cache/" + var_name + "OUT.mp4") os.remove(var_temp_path + ".avi") return "Detect Finish!"
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 test_video_detection_retinanet(clear_keras_session): detector = VideoObjectDetection() detector.setModelTypeAsRetinaNet() detector.setModelPath(model_path=os.path.join(main_folder, "data-models", "resnet50_coco_best_v2.0.1.h5")) detector.loadModel(detection_speed="fastest") video_path = detector.detectObjectsFromVideo(input_file_path=video_file, output_file_path=video_file_output, save_detected_video=True, frames_per_second=30, log_progress=True) assert os.path.exists(video_file_output + ".avi") assert isinstance(video_path, str) os.remove(video_file_output + ".avi")
def retinanet_video(video, model, output): detector = VideoObjectDetection() detector.setModelTypeAsRetinaNet() detector.setModelPath(model) detector.loadModel() start_time = time.time() video_path = detector.detectObjectsFromVideo(input_file_path=video, output_file_path=output, frames_per_second=29, log_progress=True) print(video_path) print("Total time: %s seconds" % (time.time() - start_time))
def detecting(): video_detector = VideoObjectDetection() video_detector.setModelTypeAsRetinaNet() video_detector.setModelPath(os.path.join(execution_path, "resnet50_coco_best_v2.0.1_4.h5")) # video_detector.setModelPath(os.path.join(execution_path, "v2.h5")) video_detector.loadModel() vidcap = VideoCap() camera = vidcap.camera() video_path = video_detector.detectObjectsFromVideo(camera_input=camera, save_detected_video = False, frames_per_second=20, log_progress=True, minimum_percentage_probability=30, per_frame_function=forFrame, return_detected_frame=True)
def getVideo(request): if request.method == "POST": try: file = request.FILES['sentFile'] # except: return render(request, 'homepage.html') import uuid unique_filename = str(uuid.uuid4()) file_name = default_storage.save(unique_filename, file) # Reading file from storage file = default_storage.open(file_name) file_url = default_storage.url(file_name) detector = VideoObjectDetection() detector.setModelTypeAsRetinaNet() path = os.path.join(settings.MODELS, "resnet50_coco_best_v2.0.1.h5") detector.setModelPath(path) detector.loadModel(detection_speed='faster') item = set() def forFrame(frame_number, output_array, output_count): for i in output_array: item.add(i['name']) video_path = detector.detectObjectsFromVideo(input_file_path='media/'+unique_filename, output_file_path='media/imagetest_video' , frames_per_second=20, log_progress=True, minimum_percentage_probability=30,save_detected_video=True,per_frame_function=forFrame) categoryName = objectCategory.objects.filter(object__in=item).values_list('categoryName', flat=True) ac = AdvertisementCategory.objects.filter(id__in=categoryName) ads = Advertisement.objects.filter(categoryName__in=ac) if len(ads)==0: return render(request, 'failPredictions.html') context = { 'ac': ac, 'ads': ads, } return render(request, 'vid_predictions.html',context) else: return render(request, 'homepage2.html')
class itemVideoSaveFile(): def __init__(self,setModelPath): self.execution_path = os.getcwd() self.detetor = VideoObjectDetection() self.detetor.setModelTypeAsRetinaNet() self.detetor.setModelPath(os.path.join(self.execution_path,setModelPath)) self.detetor.loadModel() def items_VideoSaveFile(self, inputFile, outputFile): video_path = self.detetor.detectCustomObjectsFromVideo(input_file_path=os.path.join(self.execution_path,inputFile), output_file_path=os.path.join(self.execution_path, outputFile), frames_per_second=20, log_progress=True) print(video_path)
def videoIdentify(input_dir, output_dir): execution_path = os.getcwd() detector = VideoObjectDetection() detector.setModelTypeAsRetinaNet() detector.setModelPath( os.path.join(execution_path, "resnet50_coco_best_v2.0.1.h5")) detector.loadModel() video_path = detector.detectObjectsFromVideo(input_file_path=input_dir, output_file_path=output_dir, frames_per_second=20, log_progress=True) print(video_path)
def car_type_more_tell_video(path): execution_path = os.getcwd() detector = VideoObjectDetection() detector.setModelTypeAsRetinaNet() detector.setModelPath( os.path.join(execution_path, "resnet50_coco_best_v2.0.1.h5")) detector.loadModel("fast") detector.detectObjectsFromVideo( input_file_path=os.path.join(execution_path, path), output_file_path=os.path.join(execution_path, "test"), frames_per_second=20, log_progress=True) print("success")
def start(self): detector = VideoObjectDetection() detector.setModelTypeAsRetinaNet() detector.setModelPath( os.path.join(self.execution_path, "resnet50_coco_best_v2.0.1.h5")) detector.loadModel() detector.detectObjectsFromVideo(camera_input=self.cap, output_file_path=os.path.join( self.execution_path, "video_frame_analysis"), frames_per_second=30, per_frame_function=forFrame, minimum_percentage_probability=70, return_detected_frame=True)
def videoDetect(): input_path = input_path_entry.get() input_path_entry.delete(0, tk.END) output_path = output_path_entry.get() output_path_entry.delete(0, tk.END) model_path = "./models/resnet50_coco_best_v2.0.1.h5" detector = VideoObjectDetection() detector.setModelTypeAsRetinaNet() detector.setModelPath(model_path) detector.loadModel() video_path = detector.detectObjectsFromVideo( input_file_path=input_path, output_file_path=os.path.join(output_path, "output_video"), frames_per_second=20, log_progress=True, minimum_percentage_probability=40)
def cameraDetect(): input_path = input_path_entry.get() input_path_entry.delete(0, tk.END) output_path = output_path_entry.get() output_path_entry.delete(0, tk.END) model_path = "./models/resnet50_coco_best_v2.0.1.h5" camera = cv2.VideoCapture(0) detector = VideoObjectDetection() detector.setModelTypeAsRetinaNet() detector.setModelPath(model_path) detector.loadModel() video_path = detector.detectObjectsFromVideo( camera_input=camera, output_file_path=output_path + "camera_detected_video", frames_per_second=20, log_progress=True, minimum_percentage_probability=40)
def demo03(): # https://github.com/OlafenwaMoses/ImageAI/blob/master/imageai/Detection/VIDEO.md from imageai.Detection import VideoObjectDetection import os execution_path = os.getcwd() detector = VideoObjectDetection() detector.setModelTypeAsRetinaNet() # https://github.com/OlafenwaMoses/ImageAI/releases/download/1.0/resnet50_coco_best_v2.0.1.h5 detector.setModelPath( r"E:\bigdata\ai\imageai\resnet50_coco_best_v2.0.1.h5" ) #(os.path.join(execution_path, "resnet50_coco_best_v2.0.1.h5")) detector.loadModel() video_path = detector.detectObjectsFromVideo( input_file_path=os.path.join(execution_path, "AInextcon.mp4"), output_file_path=os.path.join(execution_path, "AInextcon_detected"), frames_per_second=20, log_progress=True) print(video_path)
def detect_objects_resnet(filename, fps=30): # 0,1 is recording from the camera camera = cv2.VideoCapture(0) detector = VideoObjectDetection() detector.setModelTypeAsRetinaNet() detector.setModelPath( os.path.join(os.getcwd(), "models/resnet50_coco_best_v2.0.1.h5")) detector.loadModel() plt.show() video_path = detector.detectObjectsFromVideo( camera_input=camera, output_file_path=filename, frames_per_second=fps, log_progress=True, per_second_function=forSecond, return_detected_frame=True, minimum_percentage_probability=40) camera.release() # Close the window / Release webcam # De-allocate any associated memory usage cv2.destroyAllWindows()
def sender(): print("Starting detection") # eventlet.monkey_patch() # sio.start_background_task(detecting) video_detector = VideoObjectDetection() video_detector.setModelTypeAsRetinaNet() video_detector.setModelPath( os.path.join(execution_path, "resnet50_coco_best_v2.0.1_4.h5")) # video_detector.setModelPath(os.path.join(execution_path, "v2.h5")) video_detector.loadModel() vidcap = VideoCap() camera = vidcap.camera() video_path = video_detector.detectObjectsFromVideo( camera_input=camera, save_detected_video=False, frames_per_second=20, log_progress=True, minimum_percentage_probability=30, per_frame_function=forFrame, return_detected_frame=True)
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, )
def detect_objects(input_file_path, detection_confidence_threshold=60): # detection_confidence_threshold (int between 1 and 99) is the minimum detector confidence needed to include an object # Create object detector based on RetinaNet and load model weights detector = VideoObjectDetection() detector.setModelTypeAsRetinaNet() detector.setModelPath('resnet50_coco_best_v2.0.1.h5') detector.loadModel() all_frames = [] # Generates a record of all objects detected def forFrame(frame_number, output_array, output_count): all_frames.append(output_array) if frame_number % 100 == 0: print(input_file_path + ' Frame ' + str(frame_number)) detector.detectObjectsFromVideo( input_file_path=input_file_path, output_file_path='labeled_video', frames_per_second=30, per_frame_function=forFrame, minimum_percentage_probability=detection_confidence_threshold) return all_frames
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.0.1.h5")) detector.loadModel(detection_speed="flash") custom_objects = detector.CustomObjects(person=True, bicycle=True, motorcycle=True) video_path = detector.detectCustomObjectsFromVideo(custom_objects=custom_objects, input_file_path=os.path.join(execution_path, "traffic-small.mp4"), output_file_path=os.path.join(execution_path, "traffic_small_custom_flash_detected") , 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 , "Resnet.h5")) detector.loadModel("fast") video_path = detector.detectObjectsFromVideo(input_file_path=os.path.join(execution_path, "./video/NEED.mp4"), output_file_path=os.path.join(execution_path, "traffic_detected") , frames_per_second=1, log_progress=True) print(video_path)
class appearance(): def load_model(self): from imageai.Detection import VideoObjectDetection # Program starts from model loading for image recognition. Object detection from all cameras use a model loaded here. #print("Model loading is about to start. Please wait...") #print(tf.__version__) try: self.label_loading = tk.Label(self.frame, text="Loading...", bg=self.bgcolor, fg="#9EF79F") self.label_loading.config(font=("Courier", 16)) self.label_loading.place(relx=0.0, rely=0.0, relwidth=1, relheight=1) except: () # AI MODEL LOADING # Getting AI information from DB _sqlite.getAI() for row in _sqlite.getAI.result: self.ai_model_type = row[1] self.ai_model_file = row[2] self.ai_detection_speed = row[3] self.ai_minimum_percentage = row[4] #print("Model type: " + self.ai_model_type + " File: " + self.ai_model_file + " Speed: " + self.ai_detection_speed + " Percentage: " + self.ai_minimum_percentage) self.execution_path = os.getcwd() self.video_detector = VideoObjectDetection() if self.ai_model_type == "setModelTypeAsTinyYOLOv3": self.video_detector.setModelTypeAsTinyYOLOv3() elif self.ai_model_type == "setModelTypeAsYOLOv3": self.video_detector.setModelTypeAsYOLOv3() elif self.ai_model_type == "setModelTypeAsRetinaNet": self.video_detector.setModelTypeAsRetinaNet() self.video_detector.setModelPath(os.path.join(self.execution_path, self.ai_model_file)) self.video_detector.loadModel(detection_speed=self.ai_detection_speed) # Tensorflow graph used for loading a model should be the same as we use for object detection in AIMain function. global graph graph = tf.get_default_graph() if 'label_loading' in dir(self): self.label_loading.destroy() def program_start(self): # Getting VISUAL information from DB _sqlite.getVisual() for row in _sqlite.getVisual.result: self.schemename = row[1] self.transparency = row[2] self.bgcolor = row[3] # Root window self.root = tk.Tk() self.root.attributes('-alpha', self.transparency) self.root.iconbitmap(default='icon.ico') self.root.title('Terra Object Detection') # container with its initial values HEIGHT = 620 WIDTH = 1100 self.canvas = tk.Canvas(self.root, height=HEIGHT, width=WIDTH, bg="blue") # real canvas for use self.canvas.pack() # menu on a top self.menubar() # main program's screen self.main_screen(program_just_started=True) #MENUBAR# def menubar(self): menubar = tk.Menu(self.root) self.root.config(menu=menubar) settingsMenu = tk.Menu(menubar, tearoff=0) menubar.add_cascade(label="Settings", menu=settingsMenu) settingsMenu.add_command(label="Visual Settings", command=self.visual_settings) settingsMenu.add_command(label="Cameras Settings", command=self.cameras_settings) settingsMenu.add_command(label="Sound Settings", command=self.sound_settings) settingsMenu.add_command(label="Artificial Intelligence Settings", command=self.ai_settings) programMenu = tk.Menu(menubar, tearoff=0) menubar.add_cascade(label="Program", menu=programMenu) programMenu.add_command(label="Disconnect All Cameras/Refresh Screen", command=lambda: [_f.functionality().videoCaptureStopAll(), self.main_screen(program_just_started=False)]) programMenu.add_command(label="About Program", command=self.about) programMenu.add_command(label="Exit", command=self.program_exit) #MAIN SCREEN# def main_screen(self, program_just_started): # Getting SOUND information from DB _sqlite.getSound() for row in _sqlite.getSound.result: self.soundname = row[1] self.soundfile = row[2] #print("Sound: " + self.soundname + " Title: " + self.soundfile) if 'frame' in dir(self): self.frame.destroy() # destroying frame if it existed before (in case if main_screen # is not launching the first time but is being refreshed). Necessary in order to # 'forget' all old buttons and other widgets that are placed within that frame # Frame for all widgets on a main screen self.frame = tk.Frame(self.root, bg=self.bgcolor) self.frame.place(relx=0.00, rely=0.00, relwidth=1, relheight=1) if program_just_started == True: # Calling a function to load an AI model self.thread = threading.Thread(target=self.load_model, args=()) self.thread.start() else: () # Calling function to get a cameras list from DB _sqlite.getCameras() # Creating buttons for each camera for row in _sqlite.getCameras.result: # Creating necessary variables for each database output globals()["id" + str(row[0])] = row[0] globals()["name" + str(row[0])] = row[1] globals()["ipcam_streaming_url" + str(row[0])] = row[2] _sqlite.connClose() # Initial buttons - pressing an initial button initiates a video capture from camera # Every initial button has an id (camnum) which is passed to buttonsWhenCameraOn function for the purpose # to create additional buttons next to initial ones for enabling functionality to each camera (location of later generated # buttons depends on which initial button was pressed - id1, id2, id3...) ttk.Style().map("C.TButton", background=[('pressed', 'yellow')], foreground=[('pressed', 'black')]) if 'id1' in globals(): if name1 != "" and ipcam_streaming_url1 != "": button1 = ttk.Button(self.frame, text=name1, style="C.TButton", command=lambda: _f.functionality().videoCaptureStart(id1, name1, ipcam_streaming_url1, self.execution_path, self.video_detector, graph, self.ai_minimum_percentage, self.bgcolor, self.soundfile)) #command=_f.functionality().videoCaptureStart button1.place(relx=0.01, rely=0.05, relwidth=0.10, relheight=0.05) if 'id2' in globals(): if name2 != "" and ipcam_streaming_url2 != "": button2 = ttk.Button(self.frame, text=name2, style="C.TButton", command=lambda: _f.functionality().videoCaptureStart(id2, name2, ipcam_streaming_url2, self.execution_path, self.video_detector, graph, self.ai_minimum_percentage, self.bgcolor, self.soundfile)) button2.place(relx=0.01, rely=0.12, relwidth=0.10, relheight=0.05) if 'id3' in globals(): if name3 != "" and ipcam_streaming_url3 != "": button3 = ttk.Button(self.frame, text=name3, style="C.TButton", command=lambda: _f.functionality().videoCaptureStart(id3, name3, ipcam_streaming_url3, self.execution_path, self.video_detector, graph, self.ai_minimum_percentage, self.bgcolor, self.soundfile)) button3.place(relx=0.01, rely=0.19, relwidth=0.10, relheight=0.05) if 'id4' in globals(): if name4 != "" and ipcam_streaming_url4 != "": button4 = ttk.Button(self.frame, text=name4, style="C.TButton", command=lambda: _f.functionality().videoCaptureStart(id4, name4, ipcam_streaming_url4, self.execution_path, self.video_detector, graph, self.ai_minimum_percentage, self.bgcolor, self.soundfile)) button4.place(relx=0.01, rely=0.26, relwidth=0.10, relheight=0.05) if 'id5' in globals(): if name5 != "" and ipcam_streaming_url5 != "": button5 = ttk.Button(self.frame, text=name5, style="C.TButton", command=lambda: _f.functionality().videoCaptureStart(id5, name5, ipcam_streaming_url5, self.execution_path, self.video_detector, graph, self.ai_minimum_percentage, self.bgcolor, self.soundfile)) button5.place(relx=0.01, rely=0.33, relwidth=0.10, relheight=0.05) if 'id6' in globals(): if name6 != "" and ipcam_streaming_url6 != "": button6 = ttk.Button(self.frame, text=name6, style="C.TButton", command=lambda: _f.functionality().videoCaptureStart(id6, name6, ipcam_streaming_url6, self.execution_path, self.video_detector, graph, self.ai_minimum_percentage, self.bgcolor, self.soundfile)) button6.place(relx=0.01, rely=0.40, relwidth=0.10, relheight=0.05) if 'id7' in globals(): if name7 != "" and ipcam_streaming_url7 != "": button7 = ttk.Button(self.frame, text=name7, style="C.TButton", command=lambda: _f.functionality().videoCaptureStart(id7, name7, ipcam_streaming_url7, self.execution_path, self.video_detector, graph, self.ai_minimum_percentage, self.bgcolor, self.soundfile)) button7.place(relx=0.01, rely=0.47, relwidth=0.10, relheight=0.05) if 'id8' in globals(): if name8 != "" and ipcam_streaming_url8 != "": button8 = ttk.Button(self.frame, text=name8, style="C.TButton", command=lambda: _f.functionality().videoCaptureStart(id8, name8, ipcam_streaming_url8, self.execution_path, self.video_detector, graph, self.ai_minimum_percentage, self.bgcolor, self.soundfile)) button8.place(relx=0.01, rely=0.54, relwidth=0.10, relheight=0.05) button_openFolder = ttk.Button(self.frame, text="Open Detections Folder", command=lambda: os.system("start frames\ ")) button_openFolder.place(relx=0.45, rely=0.87, relwidth=0.50, relheight=0.05) # set a callback to handle when the main window is closed; trying to close all working cameras if user initiates program closure (DOESN'T WORK YET) self.root.wm_protocol("WM_DELETE_WINDOW", self.program_exit) # runs application self.root.mainloop() def visual_settings(self): visual_window = tk.Toplevel() visual_window.geometry('260x180+180+80') visual_window.attributes('-alpha', self.transparency) visual_window.resizable(width=False, height=False) visual_window.title('Visual Settings') canvas = tk.Canvas(visual_window, height=180, width=260) canvas.pack() frame = tk.Frame(visual_window, bg=self.bgcolor) frame.place(relx=0.00, rely=0.00, relwidth=1, relheight=1) label1 = tk.Label(frame, text="Current scheme: " + self.schemename, anchor='center') label1.place(relx=0.01, rely=0.05, relwidth=0.98, relheight=0.14) label2 = tk.Label(frame, text="Choose scheme:", anchor='w') label2.place(relx=0.01, rely=0.24, relwidth=0.48, relheight=0.14) Entry1List = [self.schemename, "Standard Dark", "Standard Bright", "Dark Transparent", "Navy Transparent"] entry1 = tk.StringVar(frame) entry1.set(Entry1List[0]) opt = ttk.OptionMenu(frame, entry1, *Entry1List) opt.place(relx=0.51, rely=0.24, relwidth=0.48, relheight=0.14) button = ttk.Button(frame, text="Apply", command=lambda: [_sqlite.updateVisual(entry1.get()), visual_window.destroy()]) button.place(relx=0.51, rely=0.43, relwidth=0.48, relheight=0.14) label3 = tk.Label(frame, text="Important: you must restart the program \n for the visual changes to take effect.", anchor='center') label3.place(relx=0.01, rely=0.62, relwidth=0.98, relheight=0.30) def cameras_settings(self): cameras_window = tk.Toplevel() cameras_window.grab_set() #this forces all focus on the this top level window until it is closed cameras_window.geometry('650x400+180+80') cameras_window.attributes('-alpha', self.transparency) cameras_window.resizable(width=False, height=False) cameras_window.title('Cameras Settings') canvas = tk.Canvas(cameras_window, height=400, width=650) canvas.pack() frame = tk.Frame(cameras_window, bg=self.bgcolor) frame.place(relx=0.00, rely=0.00, relwidth=1, relheight=1) label1 = tk.Label(frame, text="Id") label1.place(relx=0.01, rely=0.05, relwidth=0.20, relheight=0.05) label2 = tk.Label(frame, text="Camera Name") label2.place(relx=0.01, rely=0.12, relwidth=0.20, relheight=0.05) label3 = tk.Label(frame, text="Camera address") label3.place(relx=0.01, rely=0.19, relwidth=0.20, relheight=0.05) # We allow to choose camera id from the list (range 1-8), not to type any number Entry1List = ["1", "1", "2", "3", "4", "5", "6", "7", "8"] entry1 = tk.StringVar(frame) entry1.set(Entry1List[0]) opt = ttk.OptionMenu(frame, entry1, *Entry1List) opt.place(relx=0.22, rely=0.05, relwidth=0.20, relheight=0.05) entry2 = tk.Entry(frame) entry2.place(relx=0.22, rely=0.12, relwidth=0.20, relheight=0.05) entry3 = tk.Entry(frame) entry3.place(relx=0.22, rely=0.19, relwidth=0.20, relheight=0.05) # By pressing button, we are submitting entries to updateCameras, stopping all currently working cameras and refreshing the main_screen button = ttk.Button(frame, text="Submit", command=lambda: [_sqlite.updateCameras(entry1.get(), entry2.get(), entry3.get()), cameras_window.destroy(), self.cameras_settings(), _f.functionality().videoCaptureStopAll(), self.main_screen(program_just_started=False)]) button.place(relx=0.22, rely=0.26, relwidth=0.20, relheight=0.05) label4 = tk.Label(frame, text="Note: when you press 'Submit', all \n currently connected cameras disconnect.", anchor='center') label4.place(relx=0.01, rely=0.33, relwidth=0.41, relheight=0.15) # Showing the actual information about cameras in the database to the user _sqlite.getCameras() label = tk.Label(frame, text="Cameras information (" + str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) + ")") label.place(relx=0.45, rely=0.05, relwidth=0.54, relheight=0.05) # Inserting cameras addresses to the list rely = 0.11 for row in _sqlite.getCameras.result: id_label = tk.Label(frame, text=(row[0])) id_label.place(relx=0.45, rely=rely, relwidth=0.05, relheight=0.05) name_label = tk.Label(frame, text=(row[1])) name_label.place(relx=0.51, rely=rely, relwidth=0.20, relheight=0.05) ipcam_streaming_url_entry = tk.Entry(frame) ipcam_streaming_url_entry.configure(state='normal') ipcam_streaming_url_entry.insert(0, (row[2])) ipcam_streaming_url_entry.configure(state='readonly') ipcam_streaming_url_entry.place(relx=0.72, rely=rely, relwidth=0.27, relheight=0.05) rely += 0.06 _sqlite.connClose() def sound_settings(self): sound_window = tk.Toplevel() sound_window.geometry('260x210+180+80') sound_window.attributes('-alpha', self.transparency) sound_window.resizable(width=False, height=False) sound_window.title('Sound Settings') canvas = tk.Canvas(sound_window, height=180, width=260) canvas.pack() frame = tk.Frame(sound_window, bg=self.bgcolor) frame.place(relx=0.00, rely=0.00, relwidth=1, relheight=1) label1 = tk.Label(frame, text="Current alarm sound: " + self.soundname, anchor='center') label1.place(relx=0.01, rely=0.04, relwidth=0.98, relheight=0.12) label2 = tk.Label(frame, text="Choose sound:", anchor='w') label2.place(relx=0.01, rely=0.20, relwidth=0.48, relheight=0.12) Entry1List = [self.soundname, "alarm", "clang", "crow", "glass", "harp_run", "meadow_lark", "trolley_bell"] entry1 = tk.StringVar(frame) entry1.set(Entry1List[0]) opt = ttk.OptionMenu(frame, entry1, *Entry1List) opt.place(relx=0.51, rely=0.20, relwidth=0.48, relheight=0.12) button_listen = ttk.Button(frame, text="Play selected sound", command=lambda:play_sound(entry1.get())) button_listen.place(relx=0.51, rely=0.36, relwidth=0.48, relheight=0.12) button = ttk.Button(frame, text="Apply", command=lambda: [_sqlite.updateSound(entry1.get()), sound_window.destroy(), _f.functionality().videoCaptureStopAll(), self.main_screen(program_just_started=False)]) button.place(relx=0.51, rely=0.52, relwidth=0.48, relheight=0.12) label3 = tk.Label(frame, text="Note: when you press 'Apply', all \n currently connected cameras disconnect.", anchor='center') label3.place(relx=0.01, rely=0.68, relwidth=0.98, relheight=0.26) def play_sound(sound): winsound.PlaySound("sounds/" + sound + ".wav", winsound.SND_FILENAME) def ai_settings(self): ai_window = tk.Toplevel() ai_window.grab_set() # this forces all focus on the this top level window until it is closed ai_window.geometry('500x400+180+80') ai_window.attributes('-alpha', self.transparency) ai_window.resizable(width=False, height=False) ai_window.title('Artificial Intelligence Settings') canvas = tk.Canvas(ai_window, height=400, width=500) canvas.pack() frame = tk.Frame(ai_window, bg=self.bgcolor) frame.place(relx=0.00, rely=0.00, relwidth=1, relheight=1) if self.ai_model_file == "yolo-tiny.h5": ai_model = "TinyYOLOv3" elif self.ai_model_file == "yolo.h5": ai_model = "YOLOv3" elif self.ai_model_file == "resnet50_coco_best_v2.0.1.h5": ai_model = "RetinaNet" label1 = tk.Label(frame, text="Current parameters: " + ai_model + "; " + self.ai_detection_speed + "; " + self.ai_minimum_percentage + ".", anchor='center') label1.place(relx=0.01, rely=0.05, relwidth=0.98, relheight=0.05) label2 = tk.Label(frame, text="AI Model") label2.place(relx=0.01, rely=0.12, relwidth=0.48, relheight=0.05) label3 = tk.Label(frame, text="Detection Speed") label3.place(relx=0.01, rely=0.19, relwidth=0.48, relheight=0.05) label4 = tk.Label(frame, text="Minimum Percentage") label4.place(relx=0.01, rely=0.26, relwidth=0.48, relheight=0.05) # We allow to choose ai model from the list Entry1List = [ai_model, "TinyYOLOv3", "YOLOv3"] # "RetinaNet" - program supports it but is not included here (reasons: loads slowly, detects slowly, low detection confidence, does not work after disconnect) entry1 = tk.StringVar(frame) entry1.set(Entry1List[0]) opt1 = ttk.OptionMenu(frame, entry1, *Entry1List) opt1.place(relx=0.51, rely=0.12, relwidth=0.48, relheight=0.05) Entry2List = [self.ai_detection_speed, "normal", "fast", "faster", "fastest", "flash"] entry2 = tk.StringVar(frame) entry2.set(Entry2List[0]) opt2 = ttk.OptionMenu(frame, entry2, *Entry2List) opt2.place(relx=0.51, rely=0.19, relwidth=0.48, relheight=0.05) Entry3List = [self.ai_minimum_percentage, "20", "25", "30", "35", "40", "45", "50", "55", "60", "65", "70", "75", "80", "85", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99"] entry3 = tk.StringVar(frame) entry3.set(Entry3List[0]) opt3 = ttk.OptionMenu(frame, entry3, *Entry3List) opt3.place(relx=0.51, rely=0.26, relwidth=0.48, relheight=0.05) # By pressing button, we are submitting entries to updateAI and closing the ai_window button = ttk.Button(frame, text="Submit", command=lambda: [_sqlite.updateAI(entry1.get(), entry2.get(), entry3.get()), ai_window.destroy()]) button.place(relx=0.51, rely=0.33, relwidth=0.48, relheight=0.05) label5 = tk.Label(frame, text="Important: you must restart the program \n for the changes to take effect.", anchor='center') label5.place(relx=0.01, rely=0.40, relwidth=0.98, relheight=0.15) instructions_scrollbar = ttk.Scrollbar(frame) instructions_text = tk.Text(frame, height=4, width=50) instructions_scrollbar.place(relx=0.01, rely=0.57, relwidth=0.98, relheight=0.39) instructions_text.place(relx=0.01, rely=0.57, relwidth=0.98, relheight=0.39) instructions_scrollbar.config(command=instructions_text.yview) instructions_text.config(yscrollcommand=instructions_scrollbar.set, state=tk.NORMAL, font=("Courier", 10)) quote = """ Approximate parameters for different types of devices 1. Machines with advanced dedicated graphics (For the best performance) GeForce GTX 1050 and better AI Model: YOLOv3; Detection Speed: normal; Minimum Percentage: >90 2. Machines with dedicated graphics (For Optimal performance) AI Model: YOLOv3 or TinyYOLOv3; Detection Speed: faster; Minimum Percentage: ~50 3. Machines with integrated graphics (For increasing detection speed) Due to limited resources, only the low performance mode may be available. It is not recommended to connect more than 1 or 2 cameras simultaneously. AI Model: TinyYOLOv3; Detection Speed: flash; Minimum Percentage: ~20 """ instructions_text.insert(tk.END, quote) instructions_text.config(yscrollcommand=instructions_scrollbar.set, state=tk.DISABLED) # After entering text, making DISABLED def about(self): about_window = tk.Toplevel() about_window.geometry('500x300+180+80') about_window.attributes('-alpha', self.transparency) about_window.resizable(width=False, height=False) about_window.title('Terra Object Detection') canvas = tk.Canvas(about_window, height=120, width=240) canvas.pack() frame = tk.Frame(about_window, bg=self.bgcolor) frame.place(relx=0.00, rely=0.00, relwidth=1, relheight=1) self.label1= tk.Label(frame, text="Terra Object Detection \n v. 0.9.0 Beta \n [email protected]", anchor='center', bg=self.bgcolor, fg="#9EF79F") self.label1.config(font=("Courier", 12)) self.label1.place(relx=0.25, rely=0.35, relwidth=0.5, relheight=0.3) #self.label2 = tk.Label(frame, text="*****@*****.**", anchor='n', bg=self.bgcolor, fg="red") #self.label2.config(font=("Courier", 10)) #self.label2.place(relx=0.0, rely=0.5, relwidth=1, relheight=0.5) def program_exit(self): # self.root.destroy - destroys root but keeps cameras loops working, shouldn't be used sys.exit()
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)
# -*- coding: utf-8 -*- """ Created on Thu Sep 19 07:32:32 2019 @author: Gayatri """ #import cv2 import os from imageai.Detection import VideoObjectDetection os.chdir('F:\Internship_CV\ImageAI_Model') #camera = cv2.VideoCapture(0) #ret,frame = camera.read() detector = VideoObjectDetection() detector.setModelTypeAsRetinaNet( ) #performing your object detection tasks using the pre-trained “RetinaNet” model #setModelPath() function accepts a string which must be the path to the model file you downloaded # and must corresponds to the model type you set for your object detection instance. detector.setModelPath("resnet50_coco_best_v2.0.1.h5") detector.loadModel() # parameter detection_speed (optional) video_path = detector.detectObjectsFromVideo( input_file_path="Video_Pedestrian.mp4", output_file_path="Video_Pedestrian_detected_video_1", frames_per_second=20, log_progress=True, minimum_percentage_probability=30) print(video_path)
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.setModelTypeAsRetinaNet() video_detector.setModelPath( os.path.join(execution_path, "../models/resnet50_coco_best_v2.0.1.h5")) video_detector.loadModel() plt.show() video_detector.detectObjectsFromVideo( input_file_path=os.path.join(execution_path, "traffic.mp4"), output_file_path=os.path.join(execution_path, "video_frame_analysis"), frames_per_second=20, per_frame_function=forFrame, minimum_percentage_probability=30, return_detected_frame=True)
from imageai.Detection import ObjectDetection, VideoObjectDetection import os import cv2 import rospy from std_msgs.msg import Bool, String video_detector = VideoObjectDetection() video_detector_resnet = VideoObjectDetection() camera = cv2.VideoCapture(0) model_path = "./models/yolo-tiny.h5" model_path_resnet = "./models/resnet50_coco_best_v2.0.1.h5" video_detector.setModelTypeAsTinyYOLOv3() video_detector_resnet.setModelTypeAsRetinaNet() video_detector.setModelPath(model_path) video_detector_resnet.setModelPath(model_path_resnet) #video_detector.loadModel() video_detector_resnet.loadModel() #publishers pub_bool = rospy.Publisher('detecting_object', Bool, queue_size=100) 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,