Example #1
0
    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 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)
Example #3
0
def GetObjectsList(name,expansion):
    path_yolo=os.path.abspath("yolo.h5")
    video=VideoFileClip(f"{path}.{expansion}")
    video=video.set_fps(1)
    video=video.without_audio()
    video=video.fx(vfx.speedx,5)
    video.write_videofile(f"{name}_temp.{expansion}")

    ListObjects=[]
    def forFrame(frame_number, output_array, output_count):
        
        for k in output_array:
            if k["name"] in ListObjects:
                pass
            else:
                ListObjects.append(k["name"])
        
        



    video_detector = VideoObjectDetection()
    video_detector.setModelTypeAsYOLOv3()
    video_detector.setModelPath(path_yolo)
    video_detector.loadModel()


    video_detector.detectObjectsFromVideo(input_file_path=f"{name}_temp.{expansion}", save_detected_video=False, per_frame_function=forFrame,  minimum_percentage_probability=70,log_progress=False,display_percentage_probability=False)
    os.remove(f"{name}_temp.{expansion}")
    return ListObjects
Example #4
0
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()
class VideoThread(Thread):
    opencvCamera = None;
    ShouldClose = False
    Video_Detect = None
    OutPath = VideoPath
    #OutPath = r"C:\Users\JackXu\PycharmProjects\untitled\VideoProcessing"
    def __init__(self,CameraObj):
        Thread.__init__(self)
        self.opencvCamera = CameraObj




    def run(self):
        if (self.ShouldClose) == True:
            self.exit()
        else:
            self.Video_Detect = VideoObjectDetection()
            self.Video_Detect.setModelTypeAsYOLOv3()
            #BeginTime = time.time()
            self.Video_Detect.setModelPath(ModelPath)
            #EndTime = time.time()
            #print(" How long to set and load", EndTime - BeginTime)
            #self.Video_Detect.setModelPath(r"C:\Users\JackXu\PycharmProjects\untitled\yolo.h5")
            self.Video_Detect.loadModel("fastest")
            self.Video_Detect.detectObjectsFromVideo(camera_input= self.opencvCamera,return_detected_frame=True,
                                                     output_file_path=VideoPath,
                                                      frames_per_second=30,
                                                     minimum_percentage_probability=40,
                                                     per_frame_function=showCV, save_detected_video=False)
            self.ShouldClose = True
Example #6
0
def detection(input_path, output_path, yolo_model_path):
    """The function opens the input video and goes through each frame.
       Performs object recognition for each frame by using a YOLO model
       and writes the frame including the detection to the output video.
       :param input_path:      input video path
       :type input_path:       string
       :param output_path:     output video path
       :type output_path:      string
       :param yolo_model_path: YOLO model path
       :type yolo_model_path:  string
       :return: None
    """
    detector = VideoObjectDetection()

    # this function sets the model type of the object
    # detection instance you created to the YOLOv3 model
    detector.setModelTypeAsYOLOv3()

    # this function accepts a string that must be the
    # path to the model file, it must correspond to the
    # model typeset for the object detection instance
    detector.setModelPath(yolo_model_path)

    # this function loads the model from the path given
    detector.loadModel()

    # the function performs object detection on a video
    # file or video live-feed after the model has been
    # loaded into the instance that was created
    detector.detectCustomObjectsFromVideo(input_file_path=input_path,
                                          output_file_path=output_path,
                                          frames_per_second=20,
                                          log_progress=True)
Example #7
0
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")
Example #8
0
def main():
    execution_path = os.getcwd()
    dic = {}

    def forFrame(frame_number, output_array, output_count):
        print("Frame Number : ", frame_number)
        dic[frame_number] = output_array

    video_detector = VideoObjectDetection()
    video_detector.setModelTypeAsYOLOv3()
    video_detector.setModelPath(os.path.join(execution_path, "yolo.h5"))
    video_detector.loadModel(detection_speed="fast")

    video_detector.detectObjectsFromVideo(input_file_path=os.path.join(
        execution_path, "video.mp4"),
                                          save_detected_video=False,
                                          frames_per_second=20,
                                          per_frame_function=forFrame,
                                          minimum_percentage_probability=30)
    for k, v in dic.items():
        print(str(k) + " : " + str(v))
    video_path = video_detector.detectObjectsFromVideo(
        input_file_path=os.path.join(execution_path, "video.mp4"),
        output_file_path=os.path.join(execution_path, "video_output"),
        frames_per_second=29,
        minimum_percentage_probability=30)
Example #9
0
class MyDetector:
    myDetector = None
    def __init__(self):
        execution_path = os.getcwd()
        self.myDetector = VideoObjectDetection()
        # self.myDetector.setModelTypeAsYOLOv3()
        # self.myDetector.setModelPath(os.path.join(execution_path, "yolo.h5"))
        #self.myDetector.loadModel(detection_speed="flash")

    def process_ml(self):
        execution_path = os.getcwd()
        self.myDetector = VideoObjectDetection()
        self.myDetector.setModelTypeAsYOLOv3()
        self.myDetector.setModelPath(os.path.join(execution_path, "yolo.h5"))
        self.myDetector.loadModel(detection_speed="flash")
        video_path = self.myDetector.detectObjectsFromVideo(input_file_path=os.path.join(execution_path, "input.mp4"),
                                                output_file_path=os.path.join(execution_path, "upload/output_detected_1"), frames_per_second=29,
                                                
                                                per_frame_function=forFrame,
                                                per_second_function=forSeconds,
                                                per_minute_function=forMinute,
                                                video_complete_function=forFull,
                                                
                                                minimum_percentage_probability=10,

                                                log_progress=True)
        return video_path
Example #10
0
def detect_with_imageai_yolov3():

    """
    <-- CODE IF YOU NEED TO LOAD SOME VIDEO -->
    execution_path = os.getcwd()
    detector = VideoObjectDetection()
    detector.setModelTypeAsYOLOv3()
    detector.setModelPath(os.path.join(execution_path, "weights/yolo.h5"))
    detector.loadModel()

    video_path = detector.detectObjectsFromVideo(
        input_file_path=os.path.join(execution_path, "example_video.mp4"),
        output_file_path=os.path.join(execution_path, "Yolo.mp4"),
        frames_per_second=20, log_progress=True
    )
    """

    execution_path = os.getcwd()
    camera = cv2.VideoCapture(0)
    detector = VideoObjectDetection()
    detector.setModelTypeAsYOLOv3()
    detector.setModelPath(os.path.join(execution_path, "weights/yolo.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,
                                                  log_progress=True,
                                                  minimum_percentage_probability=40)
Example #11
0
 def detectObjects(self):
     detector = VideoObjectDetection()
     detector.setModelTypeAsYOLOv3()
     detector.setModelPath("yolo.h5")
     detector.loadModel()
     video_path = detector.detectObjectsFromVideo(camera_input=self.cam, save_detected_video = True,
                                                 per_frame_function = self.forFrame, output_file_path = "temp.avi" , log_progress=True,
                                                 return_detected_frame= True)
     return
def load_model():
    global detector
    global graph
    detector = VideoObjectDetection()
    detector.setModelTypeAsYOLOv3()
    detector.setModelPath(os.path.join(execution_path, "video-yolo.h5"))
    detector.loadModel()

    graph = tf.get_default_graph()
Example #13
0
def test_video_detection_yolov3(clear_keras_session):


    detector = VideoObjectDetection()
    detector.setModelTypeAsYOLOv3()
    detector.setModelPath(model_path=os.path.join(main_folder, "data-models", "yolo.h5"))
    detector.loadModel(detection_speed="faster")
    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 detect():
    """
    Count number of people in test video per second
    """
    execution_path = os.getcwd()

    # Add your camera credential
    # camera = cv2.VideoCapture('rtsp://*****:*****@0.0.0.0')

    # Test Video
    camera = cv2.VideoCapture('test.mp4')

    def getSizeVD(vcap):
        """
        Get Size of Camera Frame
        """
        if vcap.isOpened():
            width  = vcap.get(3)
            height = vcap.get(4)
            return width*height

    video_size = getSizeVD(camera)

    def forFrame(frame_number, output_array, output_count):
        """
        Get size of person on frame
        """
        size_list = []
        for item in output_array:
            if item.get('name', '') is 'person':
                if item.get('box_points', []):
                    [x1,y1,x2,y2] = item.get('box_points', [])
                    size = (x2 - x1) * (y2 - y1)
                    size_list.append(size)
        if size_list:
            if max(size_list) > video_size * PERCENT:
                # Add your shell script
                print("Detected Closed Person")

    # Create Video Detection Module based on Yolo
    video_detector = VideoObjectDetection()
    video_detector.setModelTypeAsYOLOv3()
    video_detector.setModelPath(os.path.join(execution_path, "yolo.h5"))
    video_detector.loadModel()

    video_detector.detectObjectsFromVideo(
        camera_input=camera,
        output_file_path=os.path.join(execution_path, "test"),
        # save_detected_video=False,
        frames_per_second=10,
        per_frame_function=forFrame,
        minimum_percentage_probability=30
    )
def detect_text(file, output_name):

    detector = VideoObjectDetection()
    detector.setModelTypeAsYOLOv3()
    detector.setModelPath("yolo.h5")
    detector.loadModel()

    video_path = detector.detectObjectsFromVideo(input_file_path=file,
                                                 output_file_path=output_name,
                                                 frames_per_second=29,
                                                 log_progress=True)
    return video_path
Example #16
0
class Detection:
    def __init__(self):
        self.path = os.getcwd()

        self.detector = VideoObjectDetection()
        self.detector.setModelTypeAsYOLOv3()
        self.detector.setModelPath("ObjectDetectionTensorflow/yolo.h5")
        self.detector.loadModel(detection_speed="fast")

        self.imgDetector = ObjectDetection()
        self.imgDetector.setModelTypeAsYOLOv3()
        self.imgDetector.setModelPath("ObjectDetectionTensorflow/yolo.h5")
        self.imgDetector.loadModel()

        self.camera = cv2.VideoCapture(0)

    def liveVideo(self):
        #Live to Video
        videoPath = self.detector.detectObjectsFromVideo(
            camera_input=self.camera,
            output_file_path=os.path.join(self.path, "Loaded_Video"),
            frames_per_second=30,
            log_progress=True,
            minimum_percentage_probability=80)
        print(videoPath)
        cv2.imshow('video', videoPath)

    def liveVideoShow(self):
        #Live video detection
        while True:
            ret, frame = self.camera.read()

            img = PIL.Image.fromarray(frame)
            img.save("ObjectDetectionTensorflow/images/pic.png")

            detected = self.imgDetector.detectCustomObjectsFromImage(
                input_image="ObjectDetectionTensorflow/images/pic.png",
                output_image_path="ObjectDetectionTensorflow/images/pic.png",
                minimum_percentage_probability=40)

            for eachObject in detected:
                print(eachObject["name"], " : ",
                      eachObject["percentage_probability"], " : ",
                      eachObject["box_points"])
                print("--------------------------------")

            if cv2.waitKey(33) == ord('a'):
                break

            img = mpimg.imread("ObjectDetectionTensorflow/images/pic.png")

            cv2.imshow('video', img)
Example #17
0
def videoDetectorInit() :
    showThreadInfo("VI")
    global video_detector
    if video_detector == None :
        video_detector = VideoObjectDetection()
        video_detector.setModelTypeAsYOLOv3()
        video_detector.setModelPath(os.path.join(execution_path, "yolo.h5")) # Download the model via this link https://github.com/OlafenwaMoses/ImageAI/releases/tag/1.0
        loginfo("model startload ", datetime.datetime.now())
        video_detector.loadModel(detection_speed="normal")
        loginfo("videoDetector init: ", video_detector)
        loginfo("model loaded ", datetime.datetime.now())

    return
Example #18
0
def object_detect(time):
    execution_path = os.getcwd()
    camera = cv2.VideoCapture(0)

    detector = VideoObjectDetection()
    detector.setModelTypeAsYOLOv3()
    detector.setModelPath(os.path.join(execution_path, "yolo.h5"))
    detector.loadModel()
    video_path = 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=50,
        save_detected_video=True)
    return redirect(index)
Example #19
0
class itemCamSaveFile():
    def __init__(self, setModePath):
        self.execution_path = os.getcwd()
        self.detector = VideoObjectDetection()
        self.detector.setModelTypeAsYOLOv3()
        self.detector.setModelPath(
            os.path.join(self.execution_path, setModePath))
        self.detector.loadModel()

    def item_CamSaveFile(self, camera, outputFile):
        video_path = self.detector.detectObjectsFromVideo(
            camera_input=camera,
            output_file_path=os.path.join(self.execution_path, outputFile),
            frames_per_second=10,
            log_progress=True,
            minimum_percentage_probability=20)
        print(video_path)
Example #20
0
def start_analys_frames():
    path = 'Data/'
    file = ['yolo.h5', 'video.avi', 'frames']

    detector = VideoObjectDetection()
    detector.setModelTypeAsYOLOv3()
    detector.setModelPath(path + file[0])
    detector.loadModel()

    video_path = detector.detectObjectsFromVideo(
        input_file_path=path + file[1],
        output_file_path=path + file[2],
        minimum_percentage_probability=40,
        frames_per_second=30,
        log_progress=True)

    c.convert()
def Object_detect():
    filename = filedialog.askopenfilename(initialdir="/",
                                          title="Select file",
                                          filetypes=(("jpeg files", "*.mp4"),
                                                     ("all files", "*.*")))
    detector = VideoObjectDetection()
    detector.setModelTypeAsYOLOv3()
    detector.setModelPath(os.path.join(execution_path, "yolo.h5"))
    detector.loadModel()

    video_path = detector.detectObjectsFromVideo(
        input_file_path=filename,
        output_file_path=os.path.join(execution_path,
                                      "traffic_mini_detected_1"),
        frames_per_second=20,
        per_second_function=forSeconds,
        minimum_percentage_probability=30,
        return_detected_frame=False,
        log_progress=True)
    print(video_path)
def detect_objects_yolo(filename, fps=30):
    # 0,1 is recording from the camera
    camera = cv2.VideoCapture(0)
    detector = VideoObjectDetection()
    detector.setModelTypeAsYOLOv3()
    detector.setModelPath(os.path.join(os.getcwd(), "models/yolo.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()
Example #23
0
def test_object_default(path_in,
                        path_out,
                        suffix='object_default',
                        path_model=os.path.join(os.getcwd(),
                                                '../models/yolo.h5'),
                        speed='fast'):
    detector = VideoObjectDetection()
    detector.setModelTypeAsYOLOv3()
    detector.setModelPath(path_model)
    detector.loadModel(detection_speed=speed)  #fast, faster, fastest, flash

    print(f'starting {suffix}')
    time_0 = time.time()

    detector.detectObjectsFromVideo(input_file_path=path_in,
                                    output_file_path=f'{path_out}_{suffix}',
                                    frames_per_second=20,
                                    per_frame_function=pfh.per_frame_handler,
                                    minimum_percentage_probability=10,
                                    return_detected_frame=True)

    print(f'mode {suffix} finished, elapsed time : {time.time() - time_0}s')
Example #24
0
class VideoProcessor():
    def __init__(self):
        self.execution_path = os.getcwd()
        self.configureVideoDetector()

    def configureVideoDetector(self):
        self.detector = VideoObjectDetection()
        self.detector.setModelTypeAsYOLOv3()
        self.detector.setModelPath( os.path.join(self.execution_path , yolo_network_path))
        self.detector.loadModel()

    def detectObjectsFromVideo(self, filePath: str):
        video_file_path = filePath
        outPath = 'out/' + video_file_path.replace('/', '-')

        video_path = self.detector.detectObjectsFromVideo(input_file_path=os.path.join( self.execution_path, video_file_path),
                                        output_file_path = os.path.join(self.execution_path, outPath),
                                        frames_per_second = frames_per_second,
                                        log_progress = True)

        # Check for the information we can have processing the video
        print(video_path)
Example #25
0
    print("Output average count for unique objects in the last second: ",
          average_output_count)
    print("------------END OF A SECOND --------------")


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")

video_path = video_detector.detectObjectsFromVideo(
    input_file_path=os.path.join(execution_path, "1080p_test.mp4"),
    output_file_path=os.path.join(execution_path, "test1080"),
    frames_per_second=30,
    log_progress=True)
print(video_path)
e = time.time()
totaltime = s - e
print(totaltime)
from imageai.Detection import VideoObjectDetection
import os

execution_path = os.getcwd()

detector = VideoObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath( os.path.join(execution_path , "yolo.h5"))
detector.loadModel()

video_path = detector.detectObjectsFromVideo(input_file_path=os.path.join(execution_path, "traffic.mp4"),
                                output_file_path=os.path.join(execution_path, "traffic_detected")
                                , frames_per_second=20, log_progress=True)
print(video_path)
Example #27
0
import cv2 as cv
from imageai.Detection import VideoObjectDetection
import os

execution_path = os.getcwd()

fourcc = cv.VideoWriter_fourcc(*'XVID')
out = cv.VideoWriter('output.avi', fourcc, 29.97, (1280, 720))


def forFrame(frame_number, output_array, output_count, detected_frame):
    frame = detected_frame
    frame = cv.cvtColor(frame, cv.COLOR_BGR2RGB)
    out.write(frame)


detector = VideoObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath(os.path.join(execution_path, "yolo.h5"))
detector.loadModel()

detector.detectObjectsFromVideo(input_file_path=os.path.join(
    execution_path, "piter2.mp4"),
                                save_detected_video=False,
                                frames_per_second=29.97,
                                log_progress=True,
                                per_frame_function=forFrame,
                                return_detected_frame=True)
out.release()
Example #28
0
def predict():

    data = request.get_json(force=True)
    body = data.get('data')
    filew = body.get('file')

    print(filew)
    decoded_string = base64.b64decode(filew)
    with open('1.mp4', 'wb') as wfile:
        wfile.write(decoded_string)

    video = cv2.VideoCapture("1.mp4")

    # Find OpenCV version
    (major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')

    fps = 0

    if int(major_ver) < 3:
        fps = round(video.get(cv2.cv.CV_CAP_PROP_FPS))
        print("Frames per second using video.get(cv2.cv.CV_CAP_PROP_FPS): {0}".
              format(fps))
    else:
        fps = round(video.get(cv2.CAP_PROP_FPS))
        print(
            "Frames per second using video.get(cv2.CAP_PROP_FPS) : {0}".format(
                fps))

    video.release()

    # def incr():
    #     global varib
    #     varib = 'bbbbbbbb'

    # incr()

    # print("varib", varib)

    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 --------------")

    def forFull(output_arrays, count_arrays, average_output_count):
        #Perform action on the 3 parameters returned into the function
        # 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)
        global objects
        objects = output_arrays

    video_detector = VideoObjectDetection()
    video_detector.setModelTypeAsYOLOv3()
    video_detector.setModelPath(os.path.join(execution_path, "yolo.h5"))
    video_detector.loadModel()

    video_detector.detectObjectsFromVideo(
        input_file_path=os.path.join(execution_path, "1.mp4"),
        output_file_path=os.path.join(execution_path, "traffic_detected"),
        frames_per_second=fps,
        per_second_function=forSeconds,
        video_complete_function=forFull,
        minimum_percentage_probability=30)

    def convert_avi_to_mp4(avi_file_path, output_name):
        os.popen(
            "ffmpeg -i '{input}' -ac 2 -b:v 2000k -c:a aac -c:v libx264 -b:a 160k -vprofile high -bf 0 -strict experimental -f mp4 './static/{output}.mp4'"
            .format(input=avi_file_path, output=output_name))
        return True

    convert_avi_to_mp4("traffic_detected.avi", "converted")

    json_dump = pd.Series(objects).to_json(orient='values')

    print("==============", json_dump)

    return json_dump
Example #29
0
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()
Example #30
0
        def real_runDetection():

            video = self.video_path.get()
            output = self.location.get()
            name = self.output_name.get()
            obj = self.objects.get().lower()

            self.progress.grid(row=7,
                               column=1,
                               columnspan=2,
                               sticky="EW",
                               padx=10,
                               pady=10)
            self.progress.start()

            flag = 0

            obj_list = []

            all_obj_list = [
                "person", "bicycle", "car", "motorcycle", "airplane", "bus",
                "train", "truck", "boat", "traffic light", "fire hydrant",
                "stop_sign", "parking meter", "bench", "bird", "cat", "dog",
                "horse", "sheep", "cow", "elephant", "bear", "zebra",
                "giraffe", "backpack", "umbrella", "handbag", "tie",
                "suitcase", "frisbee", "skis", "snowboard", "sports ball",
                "kite", "baseball bat", "baseball glove", "skateboard",
                "surfboard", "tennis racket", "bottle", "wine glass", "cup",
                "fork", "knife", "spoon", "bowl", "banana", "apple",
                "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza",
                "donot", "cake", "chair", "couch", "potted plant", "bed",
                "dining table", "toilet", "sink", "tv", "laptop", "mouse",
                "remote", "keyboard", "cell phone", "microwave", "oven",
                "toaster", "hair dryer", "refrigerator", "book", "clock",
                "vase", "scissors", "teddy bear", "toothbrush"
            ]

            if not os.path.exists(video):
                flag = 1
                messagebox.showerror("Input Video",
                                     "Input video path not defined!")
                self.progress.stop()
                self.progress.grid_forget()
                self.process_button['state'] = 'normal'
                return

            if not os.path.exists(output):
                flag = 1
                messagebox.showerror("Output Video",
                                     "Output video path not defined!")
                self.progress.stop()
                self.progress.grid_forget()
                self.process_button['state'] = 'normal'
                return

            if name == "" or name == "Enter the desired video name":
                flag = 1
                messagebox.showerror("Output Video",
                                     "Output video name not defined!")
                self.progress.stop()
                self.progress.grid_forget()
                self.process_button['state'] = 'normal'
                return

            for x in all_obj_list:
                if x != obj:
                    flag = 2
                else:
                    flag = 0
                    break

            if flag == 2:
                messagebox.showerror(
                    "Objects Error",
                    "Object values do not match with any recognised objects!")
                self.progress.stop()
                self.progress.grid_forget()
                self.process_button['state'] = 'normal'
                return

            if flag == 0:

                try:
                    execution_path = os.getcwd()

                    detector = VideoObjectDetection()
                    detector.setModelTypeAsYOLOv3()
                    detector.setModelPath("yolo.h5")
                    detector.loadModel(detection_speed="flash")

                    def saveFrames():

                        brackets = []
                        index = -1

                        for i in range(0, totalFrames):

                            if i % video_fps == 0:
                                index += 1
                                brackets.append([])
                                brackets[index].append(i + 1)

                            else:
                                brackets[index].append(i + 1)

                        index = 0

                        cap = cv.VideoCapture(video)

                        for i in obj_list:

                            if index > len(brackets):
                                break

                            for j in brackets[index]:

                                if i == j:

                                    cap.set(1, i - 1)
                                    ret, frame = cap.read()
                                    cv.imwrite(
                                        os.path.join(output,
                                                     str(i) + ".jpg"), frame)

                                    index += 1
                                    break

                        cap.release()

                    def forFrames(frame_number, output_array, output_count):

                        self.frame_processing.set("Processing frame: " +
                                                  str(frame_number))

                        for key in output_count:
                            if key == obj:
                                obj_list.append(frame_number)

                    ouput = detector.detectObjectsFromVideo(
                        input_file_path=video,
                        output_file_path=os.path.join(output, name),
                        frames_per_second=video_fps,
                        log_progress=True,
                        per_frame_function=forFrames,
                        minimum_percentage_probability=30)

                    self.frame_processing.set(
                        "Processing done, now saving matched frames if any...")

                    saveFrames()

                    self.progress.stop()
                    self.progress.grid_forget()
                    self.frame_label.grid_forget()
                    messagebox.showinfo(
                        "Processing Success",
                        "Video was successfully processed and saved in desired location, to open it click on Open Folder."
                    )
                    self.process_button['state'] = 'normal'

                except:
                    self.progress.stop()
                    self.progress.grid_forget()
                    self.frame_label.grid_forget()
                    self.process_button['state'] = 'normal'
                    messagebox.showerror("Processing Error",
                                         "Processing failed!")
        this_colors.append(color_index[eachItem])

    global resized

    if (resized == False):
        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)



video_detector = VideoObjectDetection()
video_detector.setModelTypeAsYOLOv3()
video_detector.setModelPath(os.path.join(execution_path, "yolo.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)
Example #32
0
dt = ObjectDetection()
dt.setModelTypeAsRetinaNet()
dt.setModelPath(os.path.join(exec_path, "resnet50_coco_best_v2.1.0.h5"))
dt.loadModel()

list = dt.detectCustomObjectsFromImage(
    input_image=os.path.join(exec_path, "Firenze.jpg"), 
    output_image_path=os.path.join(exec_path, "new2.jpg"),
    minimum_percentage_probability=50,
    display_percentage_probability=False,
    display_object_name=True
    )
'''

from imageai.Detection import VideoObjectDetection
import os

execution_path = os.getcwd()

dt = VideoObjectDetection()
dt.setModelTypeAsYOLOv3()
dt.setModelPath(os.path.join(execution_path, "yolo.h5"))
dt.loadModel()

video_path = dt.detectObjectsFromVideo(
    input_file_path=os.path.join(execution_path, "autostrade_video.mp4"),
    output_file_path=os.path.join(execution_path, "traffic1"),
    frames_per_second=20,
    log_progress=True)

print(video_path)