def car_type_more_tell(img):
    """
    汽车类型识别(多车):
    :param img: cv2格式
    :return: 返回预测图片, 和各类车的种类
    """
    type_dict = {
        "car_count": 0,
        "bus_count": 0,
        "motorcycle_count": 0,
        "bicycle_count": 0,
        "truck_count": 0
    }

    execution_path = os.getcwd()
    detector = ObjectDetection()
    detector.setModelTypeAsRetinaNet()
    detector.setModelPath(
        os.path.join(execution_path, "resnet50_coco_best_v2.0.1.h5"))
    detector.loadModel()
    detections_img, detections_list = detector.detectObjectsFromImage(
        input_image=img, input_type="array", output_type="array")
    for eachObject in detections_list:
        if eachObject["name"] == 'car':
            type_dict["car_count"] += 1
        if eachObject["name"] == 'truck':
            type_dict["truck_count"] += 1
        if eachObject["name"] == 'bus':
            type_dict["bus_count"] += 1
        if eachObject["name"] == 'motorcycle':
            type_dict["motorcycle_count"] += 1
        if eachObject["name"] == 'bicycle':
            type_dict["bicycle_count"] += 1
    return detections_img, type_dict
def main():

    count = 0
    execution_source = os.getcwd()

    detector = ObjectDetection()
    detector.setModelTypeAsRetinaNet()
    detector.setModelPath(
        os.path.join(execution_source, "resnet50_coco_best_v2.0.1.h5"))
    detector.loadModel()
    detections = detector.detectObjectsFromImage(
        input_image=os.path.join(execution_source,
                                 "F:\CS\TrafficDetection\saved_img.jpg"),
        output_image_path=os.path.join(execution_source, "imagenew.jpg"))

    for eachObject in detections:
        if (eachObject["name"] == "car" or eachObject["name"] == "bus"
                or eachObject["name"] == "truck"):
            count += 1
        print(eachObject["name"], " : ", eachObject["percentage_probability"])

    print(count)
    if (count > 20):
        green.main()
    else:
        red.main()
Example #3
0
def obj_detect():

    if request.method == 'POST':
        from imageai.Detection import ObjectDetection
        model = ObjectDetection()
        model.setModelTypeAsRetinaNet()
        model.setModelPath("./model/model.h5")
        model.loadModel()
        detections, paths = model.detectObjectsFromImage(
            input_image=image_to_process,
            output_image_path=OUTPUT_PATH,
            extract_detected_objects=True)
        img_to_render = os.path.join('..', image_to_process)
        img_to_render = img_to_render.replace('\\', '/')
        paths = [os.path.join('..', path) for path in paths]
        paths = [path.replace('\\', '/') for path in paths]
        for i, path in enumerate(paths):
            detections[i]['index'] = i
            detections[i]['path'] = path
        for detection in detections:
            detection['percentage_probability'] = round(
                detection['percentage_probability'], 2)
        return render_template('result.html',
                               img_name=img_to_render,
                               img_details=detections,
                               num_obj=len(detections))
def detect():
    detector = ObjectDetection()

    model_path = "C:/Users/Prayag/PycharmProjects/Detector/models/resnet50_coco_best_v2.0.1.h5"
    input_path = "C:/Users/Prayag/PycharmProjects/Detector/input/img1.png"
    output_path = "C:/Users/Prayag/PycharmProjects/Detector/output/img.png"
    detector.setModelTypeAsRetinaNet()
    detector.setModelPath(model_path)
    detector.loadModel()
    detection = detector.detectObjectsFromImage(
        input_image=input_path,
        output_image_path=output_path,
        minimum_percentage_probability=50)
    for eachItem in detection:
        print(eachItem["name"], " : ", eachItem["percentage_probability"], ":",
              eachItem["box_points"])
    st = ""
    for x in detection:
        st = st + str(x["name"]) + " "
    print(st)

    engine = pyttsx3.init()
    engine.setProperty('rate', 150)
    engine.say("List of objects from your left to right are as follows ")
    engine.setProperty('rate', 130)
    final_st = st
    engine.say(final_st)
    engine.runAndWait()
Example #5
0
def determineProbability(img):
    print('IMAGE--------------------', img)
    exec_path = os.getcwd()
    print(exec_path)
    exec_path, end_path = os.path.split(str(exec_path))
    print(exec_path, end_path)
    outputPath = os.path.join(exec_path, 'PoacherWeb', 'media')
    exec_path = os.path.join(exec_path, 'PoacherWeb', 'media', img)
    print(f'image path: {exec_path}')
    model_path = os.getcwd()
    print(model_path)

    detector = ObjectDetection()
    detector.setModelTypeAsRetinaNet()
    detector.setModelPath(
        os.path.join(model_path, 'Model', "resnet50_coco_best_v2.1.0.h5"))
    detector.loadModel()
    detections = detector.detectObjectsFromImage(
        input_image=str(exec_path),
        output_image_path=os.path.join(outputPath, 'output-' + img))
    person_probability = 0
    for eachObject in detections:
        print(eachObject["name"], " : ", eachObject["percentage_probability"])
        if (eachObject['name'] == 'person'):
            if (eachObject['percentage_probability'] > person_probability):
                person_probability = eachObject['percentage_probability']
    print(detections)
    return [person_probability, 'output-' + img]
Example #6
0
    def imagedetection(self):
        #print("egyg")
        #global message
        #message=[]
        res = path.rsplit('/', 1)
        path1=res[1]
        res=res[0]
        print(res)
        #images = [f for f in listdir(res) if isfile(join(res, f))]
        #print(images)
        #i=0
        #for image in images: 
        x=res+"/"+path1
        print(x)
        execution_path = os.getcwd()
        detector = ObjectDetection()
        detector.setModelTypeAsRetinaNet()
        detector.setModelPath( os.path.join(execution_path , "resnet50_coco_best_v2.0.1.h5"))
        detector.loadModel()
        y="imagenew.jpg"
        print(execution_path)
        detections, extracted_images = detector.detectObjectsFromImage(input_image=os.path.join(res ,path1), output_image_path=os.path.join(res , y), extract_detected_objects=True)

        print(extracted_images)
        path2=res+"/"+y
        print(self)
        self.label.setPixmap(QtGui.QPixmap(path2))
        c=""
        for eachObject in detections:
            print(eachObject["name"] , " : " , eachObject["percentage_probability"] )
            c=c+"\n"+eachObject["name"] + " : "+str(eachObject["percentage_probability"])
            print(c)
        self.textEdit.setText(c)
Example #7
0
def hello_world():
    with graph.as_default():
        file = request.files['file']
        # file.save(os.path.join(execution_path, file.filename))
        detector = ObjectDetection()

        detector.setModelTypeAsRetinaNet()
        detector.setModelPath(
            os.path.join(execution_path, "resnet50_coco_best_v2.0.1.h5"))
        detector.loadModel()
        detector.detectObjectsFromImage(input_image=file,
                                        output_image_path=os.path.join(
                                            execution_path, file.filename))

# for eachObject in detections:
#     print(eachObject["name"], " : ", eachObject["percentage_probability"])

    return ""
Example #8
0
    def objectDetection(self, pathInputData):

        self.type = imghdr.what(pathInputData)
        if self.type == "jpeg":
            self.type = "jpg"
        if self.type not in ["jpg", "png"]:
            return None

        tempData = pathInputData[0:-4]

        pathOutputData = tempData + "out." + self.type
        zhuanhuanPic = tempData + "zh." + self.type

        # 目标图片大小
        dst_w = 400
        dst_h = 0
        # #保存的图片质量
        save_q = 40
        # 等比例压缩

        try:
            img = image.open(pathInputData)
            bg = image.new("RGB", img.size, (255, 255, 255))
            bg.paste(img, img)
            bg.save(pathInputData)
        except:
            pass

        self.resizeImg(ori_img=pathInputData,
                       dst_img=zhuanhuanPic,
                       dst_w=dst_w,
                       dst_h=dst_h,
                       save_q=save_q)

        for i in range(0, 3):
            try:
                detector = ObjectDetection()
                detector.setModelTypeAsRetinaNet()
                detector.setModelPath(
                    os.path.join(self.getExecutionPath(),
                                 "tools/imageAI/resnet50_coco_best_v2.0.1.h5"))
                detector.loadModel()
                detections = detector.detectObjectsFromImage(
                    input_image=zhuanhuanPic, output_image_path=pathOutputData)
                break
            except Exception as e:
                continue
        with open(pathOutputData, "rb") as f:
            # b64encode是编码,b64decode是解码
            base64_data = base64.b64encode(f.read()).decode("utf-8")

        result = []
        for eachObject in detections:
            result.append(eachObject["name"] + " : " +
                          eachObject["percentage_probability"])

        return json.dumps({"baseData": base64_data, "result": result})
Example #9
0
class Detector:
    def __init__(self):
        self.execution_path = os.getcwd()
        self.detector = ObjectDetection()
        self.detector.setModelTypeAsRetinaNet()
        self.detector.setModelPath(
            os.path.join(self.execution_path, "resnet50_coco_best_v2.0.1.h5"))
        self.detector.loadModel()

    def run(self, image_matrix):
        detections = self.detector.detectObjectsFromImage(
            input_type="array", input_image=image_matrix)
        results = []
        for eachObject in detections:
            pts = eachObject["box_points"]
            results.append(pts)
        return results

    def run_save(self, image_path, storage_path='detect_target.txt'):
        detections = self.detector.detectObjectsFromImage(
            input_image=os.path.join(self.execution_path, image_path))
        with open(storage_path, "w+") as f:
            for eachObject in detections:
                print(eachObject["name"], " : ",
                      eachObject["percentage_probability"], " : ",
                      eachObject["box_points"])
                pts = eachObject["box_points"]
                f.write(
                    str(pts[0]) + ' ' + str(pts[1]) + ' ' + str(pts[2]) + ' ' +
                    str(pts[3]) + "\n")

    def run_return(self, image_path):
        detections = self.detector.detectObjectsFromImage(
            input_image=os.path.join(self.execution_path, image_path))
        result = []
        for eachObject in detections:
            pts = eachObject["box_points"]
            temp = []
            temp.append(pts[0])
            temp.append(pts[1])
            temp.append(pts[2])
            temp.append(pts[3])
            result.append(temp)
        return result
Example #10
0
def object_filtering(file_name1, file_name2, file_name3, output):
    detector = ObjectDetection()
    detector.setModelTypeAsRetinaNet()
    detector.setModelPath(
        os.path.join(execution_path, "resnet50_coco_best_v2.0.1.h5"))
    detector.loadModel()

    detections1 = detector.detectObjectsFromImage(
        input_image=os.path.join(execution_path, file_name1),
        output_image_path=os.path.join(execution_path, "detection.ppm"))
    for eachObject in detections1:
        print(eachObject["name"], ":", eachObject["percentage_probability"])
    """
    use netpbmfile to convert ppm file to 3d numpy array
    then just make manual framing of perceived foreign objects.
    Otherwise input of computer vision technology is necessary.
    """
    def padding(image, image1, image2):
        """
        calculate necessary padding and image positions for foriegn objects
        """
        padding_length = max(
            [image.shape[0], image1.shape[0], image2.shape[0]])
        padding_width = sum([image.shape[1], image1.shape[1], image2.shape[1]])
        rgb = 3
        image_colpos = image.shape[1]
        image1_colpos = image_colpos + image1.shape[1]
        image2_colpos = image1_colpos + image2.shape[1]
        return np.zeros(
            [padding_length, padding_width, rgb],
            dtype=np.int64), image_colpos, image1_colpos, image2_colpos

    # open user input files simulataneously
    with netpbmfile.NetpbmFile(file_name1) as ppm, \
    netpbmfile.NetpbmFile(file_name2) as ppm1, \
    netpbmfile.NetpbmFile(file_name3) as ppm2:

        # convert ppm to numpy array
        image, image1, image2 = ppm.asarray(), ppm1.asarray(), ppm2.asarray()
        #frame perceived foreign objects
        image = image[(image.shape[0]) // 3:((image.shape[0]) // 3) *
                      2, :(image.shape[1]) // 2]
        image1 = image1[(image1.shape[0]) // 2:(image1.shape[0] // 3) * 2,
                        (image1.shape[1]) // 3:(image1.shape[1] // 3) * 2]
        image2 = image2[(image2.shape[0]) // 8:((image2.shape[0]) // 8) * 4,
                        ((image2.shape[1] // 3)) * 2:]

        # padding and decide concantenation points for new image
        result, image_colpos, image1_colpos, image2_colpos = padding(
            image, image1, image2)
        # position foreign objects to new image
        result[:image.shape[0], :image_colpos] = image
        result[:image1.shape[0], image_colpos:image1_colpos] = image1
        result[:image2.shape[0], image1_colpos:image2_colpos] = image2
        # write to file
        netpbmfile.imwrite("{output}.ppm".format(output=output), result)
Example #11
0
def test_object_detection_tiny_yolov3_array_io():

    try:
        keras.backend.clear_session()
    except:
        None

    image_input_array = cv2.imread(image_input)

    detector = ObjectDetection()
    detector.setModelTypeAsTinyYOLOv3()
    detector.setModelPath(
        os.path.join(main_folder, "data-models", "yolo-tiny.h5"))
    detector.loadModel()
    detected_array, results = detector.detectObjectsFromImage(
        input_image=image_input_array,
        input_type="array",
        minimum_percentage_probability=40,
        output_type="array")

    assert isinstance(detected_array, ndarray)
    assert isinstance(results, list)
    for result in results:
        assert isinstance(result["name"], str)
        assert isinstance(result["percentage_probability"], float)
        assert isinstance(result["box_points"], tuple)

    detected_array, results2, extracted_arrays = detector.detectObjectsFromImage(
        input_image=image_input,
        output_image_path=image_output,
        minimum_percentage_probability=40,
        extract_detected_objects=True,
        output_type="array")

    assert isinstance(results2, list)
    assert isinstance(extracted_arrays, list)
    for result2 in results2:
        assert isinstance(result2["name"], str)
        assert isinstance(result2["percentage_probability"], float)
        assert isinstance(result2["box_points"], tuple)

    for extracted_array in extracted_arrays:
        assert isinstance(extracted_array, ndarray)
def process_frame():
    execution_path = os.getcwd()
    detector = ObjectDetection()
    detector.setModelTypeAsYOLOv3()
    detector.setModelPath(os.path.join(execution_path, "yolo.h5"))
    detector.loadModel()
    detections = detector.detectObjectsFromImage(
        input_image=os.path.join(execution_path, "captured-image.jpg"),
        output_image_path=os.path.join(execution_path, "processed-image.jpg"),
        minimum_percentage_probability=50)
Example #13
0
def object_detection_image(image_path):
    execution_path = os.getcwd()
    detector = ObjectDetection()
    detector.setModelTypeAsRetinaNet()
    detector.setModelPath(
        os.path.join(execution_path, "resources/resnet50_coco_best_v2.0.1.h5"))
    detector.loadModel()
    detections = detector.detectObjectsFromImage(
        input_image=os.path.join(execution_path, image_path))
    print(type(detections))
    print(detections[0])
    dt = {}
    for i in detections:
        if i["percentage_probability"] > 0:
            dt[i["percentage_probability"]] = [i["name"], i["box_points"]]
    ds = sorted(dt.keys())
    print(type(ds))
    ds = ds[(len(ds) - 20):len(ds)]
    for i in ds:
        print(dt[i])

# path
    path = image_path

    # Reading an image in default mode
    image = cv2.imread(path)

    # Window name in which image is displayed
    window_name = 'Image'

    # Start coordinate, here (5, 5)
    # represents the top left corner of rectangle
    start_point = (5, 5)

    # Ending coordinate, here (220, 220)
    # represents the bottom right corner of rectangle
    end_point = (220, 220)

    # Blue color in BGR
    color = (255, 0, 0)

    # Line thickness of 2 px
    thickness = 2

    # Using cv2.rectangle() method
    # Draw a rectangle with blue line borders of thickness of 2 px
    # Displaying the image
    for i in ds:
        b = (dt[i][1][0], dt[i][1][1])
        e = (dt[i][1][2], dt[i][1][3])
        cv2.rectangle(image, b, e, color, thickness)
    retval, buffer = cv2.imencode('.jpg', image)
    encoded_string = base64.b64encode(buffer)
    K.clear_session()
    return encoded_string
Example #14
0
def test_object_detection_retinanet():
    try:
        keras.backend.clear_session()
    except:
        None
    detector = ObjectDetection()
    detector.setModelTypeAsRetinaNet()
    detector.setModelPath(
        os.path.join(main_folder, "data-models",
                     "resnet50_coco_best_v2.0.1.h5"))
    detector.loadModel()
    results = detector.detectObjectsFromImage(
        input_image=image_input,
        output_image_path=image_output,
        minimum_percentage_probability=40)

    assert isinstance(results, list)
    for result in results:
        assert isinstance(result["name"], str)
        assert isinstance(result["percentage_probability"], float)
        assert isinstance(result["box_points"], ndarray)
    assert os.path.exists(image_output)
    os.remove(image_output)

    results2, extracted_paths = detector.detectObjectsFromImage(
        input_image=image_input,
        output_image_path=image_output,
        minimum_percentage_probability=40,
        extract_detected_objects=True)

    assert isinstance(results2, list)
    assert isinstance(extracted_paths, list)
    assert os.path.isdir(os.path.join(image_output + "-objects"))
    for result2 in results2:
        assert isinstance(result2["name"], str)
        assert isinstance(result2["percentage_probability"], float)
        assert isinstance(result2["box_points"], ndarray)

    for extracted_path in extracted_paths:
        assert os.path.exists(extracted_path)

    shutil.rmtree(os.path.join(image_output + "-objects"))
Example #15
0
def tinyYoloImage(image, model, output):
    detector = ObjectDetection()
    detector.setModelTypeAsTinyYOLOv3()
    detector.setModelPath(model)
    detector.loadModel()
    start_time = time.time()
    detections = detector.detectObjectsFromImage(input_image=image,
                                                 output_image_path=output)
    for eachObject in detections:
        print(eachObject["name"], " : ", eachObject["percentage_probability"])
    print("Total time: %s seconds" % (time.time() - start_time))
def object_detection():
    cwd = os.getcwd()

    detector = ObjectDetection()
    detector.setModelTypeAsRetinaNet()
    detector.setModelPath(os.path.join(cwd, "resnet50_coco_best_v2.0.1.h5"))
    detector.loadModel()
    detections = detector.detectObjectsFromImage(
        input_image=os.path.join(cwd, "resources/lucas.jpg"),
        output_image_path=os.path.join(cwd, "output.jpg"),
        minimum_percentage_probability=30)
Example #17
0
def detections(img):
    """With the help of the pre-trained
    model weights, we detect all objects
    available in the image using imageai's
    object detection class"""
    detector = ObjectDetection()
    detector.setModelTypeAsRetinaNet()
    detector.setModelPath(model_path)
    detector.loadModel()
    detections = detector.detectObjectsFromImage(image_path, os.path.join(os.getcwd(), 'detections.jpg'))
    return detections
Example #18
0
def detect_doggo():
    execution_path = os.getcwd()
    detector = ObjectDetection()
    detector.setModelTypeAsRetinaNet()
    detector.setModelPath(
        os.path.join(execution_path, "resnet50_coco_best_v2.0.1.h5"))
    detector.loadModel()
    detections = detector.detectObjectsFromImage(
        input_image=os.path.join(execution_path, "dogs.jpg"),
        output_image_path=os.path.join(execution_path, "imagenew.jpg"))
    return detections
Example #19
0
def get_rect():
    detector = ObjectDetection()
    detector.setModelTypeAsRetinaNet()
    detector.setModelPath(
        os.path.join("/home/jayasurya/Downloads",
                     'resnet50_coco_best_v2.0.1.h5'))
    detector.loadModel()
    detections = detector.detectObjectsFromImage(
        input_image=os.path.join("/home/jayasurya/Desktop/", INPUT_IMAGE),
        output_image_path=os.path.join("/home/jayasurya/Desktop/",
                                       OUTPUT_IMAGE))
    return detections
Example #20
0
def elaborazione():
    execution_path = os.getcwd()
    detector = ObjectDetection()
    detector.setModelTypeAsRetinaNet()
    detector.setModelPath(
        os.path.join(execution_path, "resnet50_coco_best_v2.0.1.h5"))
    detector.loadModel()
    detections = detector.detectObjectsFromImage(
        input_image=os.path.join(execution_path, "image1.jpg"),
        output_image_path=os.path.join(execution_path, "imagenew.jpg"))
    for eachObject in detections:
        print(eachObject["name"], " : ", eachObject["percentage_probability"])
Example #21
0
def imganalyze(pathtoimages):
    detector = ObjectDetection()
    detector.setModelTypeAsRetinaNet()
    detector.setModelPath(os.path.join(os.getcwd()+'/content_detection', "resnet50_coco_best_v2.0.1.h5"))
    detector.loadModel()
    ######
    path = f'{os.getcwd()}/content_detection/imagebank/'


    detections = detector.detectObjectsFromImage(input_image=os.path.join(path, 'downloaded_image.jpg'), output_image_path=os.path.join(path , 'Analysedimage.jpg'))

    for eachObject in detections:
        print(eachObject["name"] , " : " , eachObject["percentage_probability"] )
Example #22
0
def detect(filename):
    execution_path = os.getcwd()
    detector = ObjectDetection()
    detector.setModelTypeAsRetinaNet()
    detector.setModelPath(
        os.path.join(execution_path, "resnet50_coco_best_v2.1.0.h5"))
    detector.loadModel()
    detections = detector.detectObjectsFromImage(
        input_image=filename, output_image_path="../img-analysis/imagenew.jpg")
    allObjects = set()
    for eachObject in detections:
        allObjects.add(eachObject["name"])
    return list(allObjects)
Example #23
0
def test_object_detection_retinanet_array_io():

    image_input_array = cv2.imread(image_input)

    detector = ObjectDetection()
    detector.setModelTypeAsRetinaNet()
    detector.setModelPath(
        os.path.join(main_folder, "data-models",
                     "resnet50_coco_best_v2.1.0.h5"))
    detector.loadModel()
    detected_array, results = detector.detectObjectsFromImage(
        input_image=image_input_array,
        input_type="array",
        minimum_percentage_probability=40,
        output_type="array")

    assert isinstance(detected_array, ndarray)
    assert isinstance(results, list)
    for result in results:
        assert isinstance(result["name"], str)
        assert isinstance(result["percentage_probability"], float)
        assert isinstance(result["box_points"], list)

    detected_array, results2, extracted_arrays = detector.detectObjectsFromImage(
        input_image=image_input,
        output_image_path=image_output,
        minimum_percentage_probability=40,
        extract_detected_objects=True,
        output_type="array")

    assert isinstance(results2, list)
    assert isinstance(extracted_arrays, list)
    for result2 in results2:
        assert isinstance(result2["name"], str)
        assert isinstance(result2["percentage_probability"], float)
        assert isinstance(result2["box_points"], list)

    for extracted_array in extracted_arrays:
        assert isinstance(extracted_array, ndarray)
Example #24
0
def object_recognition(src_image, output_directory):
    execution_path = os.getcwd()
    detector = ObjectDetection()
    detector.setModelTypeAsRetinaNet()
    detector.setModelPath(
        os.path.join(execution_path, "resnet50_coco_best_v2.0.1.h5"))
    detector.loadModel()
    detections = detector.detectObjectsFromImage(
        input_image=src_image,
        output_image_path=os.path.join(output_directory,
                                       os.path.basename(src_image)))
    for eachObject in detections:
        print(eachObject["name"], " : ", eachObject["percentage_probability"])
Example #25
0
def test_object_detection_tiny_yolov3():

    detector = ObjectDetection()
    detector.setModelTypeAsTinyYOLOv3()
    detector.setModelPath(
        os.path.join(main_folder, "data-models", "yolo-tiny.h5"))
    detector.loadModel()
    results = detector.detectObjectsFromImage(
        input_image=image_input,
        output_image_path=image_output,
        minimum_percentage_probability=40)

    assert isinstance(results, list)
    for result in results:
        assert isinstance(result["name"], str)
        assert isinstance(result["percentage_probability"], float)
        assert isinstance(result["box_points"], list)
    assert os.path.exists(image_output)
    os.remove(image_output)

    results2, extracted_paths = detector.detectObjectsFromImage(
        input_image=image_input,
        output_image_path=image_output,
        minimum_percentage_probability=40,
        extract_detected_objects=True)

    assert isinstance(results2, list)
    assert isinstance(extracted_paths, list)
    assert os.path.isdir(os.path.join(image_output + "-objects"))
    for result2 in results2:
        assert isinstance(result2["name"], str)
        assert isinstance(result2["percentage_probability"], float)
        assert isinstance(result2["box_points"], list)

    for extracted_path in extracted_paths:
        assert os.path.exists(extracted_path)

    shutil.rmtree(os.path.join(image_output + "-objects"))
Example #26
0
File: routes.py Project: Lgirl/NLP
def imageAI():
    text = ""
    detector = ObjectDetection()
    detector.setModelTypeAsRetinaNet()
    detector.setModelPath(
        os.path.join(execution_path, "resnet50_coco_best_v2.0.1.h5"))
    detector.loadModel()
    detections = detector.detectObjectsFromImage(
        input_image=os.path.join(execution_path, "static/images/image.jpg"),
        output_image_path=os.path.join(execution_path,
                                       "static/images/imagenew.jpg"))
    for eachObject in detections:
        text = text + get_reuslt(translate(eachObject["name"]))
    flash(writer.cangtou(text))
Example #27
0
def findPhones():
    execution_path = os.getcwd()
    detector = ObjectDetection()
    detector.setModelTypeAsRetinaNet()
    detector.setModelPath( os.path.join(execution_path , "resnet50_coco_best_v2.0.1.h5"))
    detector.loadModel(detection_speed="faster")
    #custom_objects = detector.CustomObjects(person=True, cell_phone=True)
    while(1):
        try:
            detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path , "test45.jpg"), output_image_path=os.path.join(execution_path , "imagenew.jpg"))
            for eachObject in detections:
                print(eachObject['name'])
        except ValueError:
            print('bad file')
Example #28
0
def browse(filename):
    from imageai.Detection import ObjectDetection
    st = time.time()
    execution_path = os.getcwd()
    obj = {}
    detector = ObjectDetection()
    detector.setModelTypeAsRetinaNet()
    detector.setModelPath(
        os.path.join(execution_path, "resnet50_coco_best_v2.0.1.h5"))
    detector.loadModel()

    count = 0
    ic = 1
    base = os.path.basename(filename)
    for i in range(0, ic):
        detections, extracted = detector.detectObjectsFromImage(
            input_image=os.path.join(execution_path, "%s" % filename),
            output_image_path="img\\%s%i.jpg" % (base, i),
            extract_detected_objects=True)
        for eachObject in detections:
            if eachObject["name"] == "person":
                count = count + 1
            print(eachObject["name"], " : ",
                  eachObject["percentage_probability"])
            obj[eachObject["name"]] = eachObject["percentage_probability"]
    print("No of person is %d" % count)
    en = time.time()
    #dataframe creation

    print(detections)
    print(extracted)
    df = pd.DataFrame(detections)
    df["Extracted Img"] = extracted
    print(df)

    #creating excel sheet

    print(filename)
    base = os.path.basename(filename)
    writer = pd.ExcelWriter(r'countexcel/%s.xlsx' % base, engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    writer.save()
    print(base)

    #for i in range(0, ic):
    #os.remove(os.path.join(execution_path , "%s%s" %(filename,i)))
    #os.remove(os.path.join(execution_path , "img/imagenew_%d.jpg" %i))
    total = en - st
    print(total)
    return count, ic, obj
Example #29
0
class ObjectDetector:
    def __init__(self):
        self.detector = ObjectDetection()
        self.detector.setModelTypeAsRetinaNet()
        self.detector.setModelPath("./models/resnet50_coco_best_v2.0.1.h5")
        self.detector.loadModel()

        self.prediction = ImagePrediction()
        self.prediction.setModelTypeAsResNet()
        self.prediction.setModelPath(
            "./models/resnet50_weights_tf_dim_ordering_tf_kernels.h5")
        self.prediction.loadModel()

    def detect(self, filename, dimensions):

        print("Filename: {}".format(filename))
        detections = self.detector.detectObjectsFromImage(
            input_image=filename,
            input_type="file",
            output_image_path="./tmp/out.jpg")

        items = []

        for detection in detections:
            obj = {}
            obj["type"] = "object"
            obj["name"] = detection["name"]
            obj["probability"] = detection["percentage_probability"]
            x1, y1, x2, y2 = detection["box_points"]
            obj["box"] = {}
            obj["box"]["left"] = int(x1)
            obj["box"]["top"] = int(y1)
            obj["box"]["bottom"] = dimensions["height"] - (
                dimensions["height"] - int(y2))
            obj["box"]["right"] = dimensions["width"] - (dimensions["width"] -
                                                         int(x2))
            items.append(obj)

        predictions, probabilities = self.prediction.predictImage(
            filename, result_count=10)

        preds = []
        for idx, val in enumerate(predictions):
            obj = {}
            obj["type"] = "tag"
            obj["name"] = val
            obj["probability"] = probabilities[idx]
            items.append(obj)

        return items
Example #30
0
def handle_docs_photo(message):
    try:
        chat_id = message.chat.id

        file_info = bot.get_file(message.document.file_id)
        downloaded_file = bot.download_file(file_info.file_path)

        #проверям есть ли такой файл и удаляем его

        filePath = 'C:\Development\what_on_the_picture_bot/venv/objects.jpg'

        # As file at filePath is deleted now, so we should check if file exists or not not before deleting them
        if os.path.exists(filePath):
            os.remove(filePath)
            print("Old file deleted")
        else:
            print("Can not delete the file as it doesn't exists")

        #сохраняем файл
        src = 'C:\Development\what_on_the_picture_bot/venv/objects.jpg'  # + message.document.file_name;
        with open(src, 'wb') as new_file:
            new_file.write(downloaded_file)

        bot.reply_to(message, "Обрабатываю...")
        print("New file downloaded")

        try:
            exec_path = os.getcwd()

            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, "objects.jpg"),
                output_image_path=os.path.join(exec_path, "new_objects.jpg"),
                minimum_percentage_probability=50)
        except Exception as e:
            bot.reply_to(message, "Ошибка в модели - " + e)

        #Отправляем обработанное фото
        photo = 'C:\Development\what_on_the_picture_bot/venv/new_objects.jpg'

        bot.send_photo(message.chat.id, open(photo, 'rb'))

    except Exception as e:
        bot.reply_to(message, "Ой, возникла ошибка. Попробуйте ещё раз.")
Example #31
0
from imageai.Detection import ObjectDetection
import os
from time import time

execution_path = os.getcwd()

detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath( os.path.join(execution_path , "resnet50_coco_best_v2.0.1.h5"))
detector.loadModel(detection_speed="flash")

our_time = time()
detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path , "6.jpg"), output_image_path=os.path.join(execution_path , "6flash.jpg"), minimum_percentage_probability=30)
print("IT TOOK : ", time() - our_time)
for eachObject in detections:
    print(eachObject["name"] + " : " + eachObject["percentage_probability"] )
    print("--------------------------------")