Esempio n. 1
0
                        color = (0, 179, 0)

                    cv2.putText(
                        frame, object_name + ": " +
                        str(int(object_probability)) + "%", org, font,
                        fontscale, color, thickness)

                return ret, cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            else:
                return ret, None
        else:
            return ret, None

    # Release the video source when the object is destroyed
    def __del__(self):
        if self.vid.isOpened():
            self.vid.release()


# Create a window and pass it to the Application object
# Load dataset/model
detector = CustomObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath("manafv6.h5")
detector.setJsonPath("detection_config.json")
detector.loadModel()

window = Tk()
thread_gui = Gui(window, "Techrecycle")
thread_gui.start()
 def __init__(self, detectorModel, setJson):
     self.detector = CustomObjectDetection()
     self.detector.setModelPath(detectorModel)
     self.detector.setJsonPath(setJson)
     self.detector.loadModel()
Esempio n. 3
0
import io
import cv2
import sys
from PIL import Image
import numpy as np
from flask import Flask, request, jsonify
from imageai.Detection.Custom import CustomObjectDetection

MODEL_PATH = "../models/" + sys.argv[1]
JSON_PATH = "../json/" + sys.argv[2]

app = Flask(__name__)

dtc = CustomObjectDetection()
dtc.setModelTypeAsYOLOv3()
dtc.setModelPath(MODEL_PATH)
dtc.setJsonPath(JSON_PATH)
dtc.loadModel()


def detect_objects(img, min_prob=30):
    """Processes and transforms product images to detect objects learned
    by the model loaded in global.

    Arguments:
        img {FileStorage} -- Uploaded image file though api request.

    Keyword Arguments:
        min_prob {int} -- Threshold probability for objects detected in
        images. Low probability levels show more detections
        (default: {30}).
def detectImage(queue):

    detector = CustomObjectDetection()
    detector.setModelTypeAsYOLOv3()
    detector.setModelPath(modelFilePath)
    detector.setJsonPath("detection_config.json")
    detector.loadModel()

    while True:

        if queue.qsize() > 0:

            try:
                fileNameIn = queue.get()
                logging.info("[detectImage] Processing %s" % fileNameIn)

                filePathIn = os.path.join(imageFolderIn, fileNameIn)
                filePathOut = os.path.join(imageFolderOut, fileNameIn)
                filePathStorage = os.path.join(imageStorage, fileNameIn)
                logging.info(filePathOut)
                filePathSpare = os.path.join(spareImageFolder, fileNameIn)
                filepathBorder = os.path.join(borderImageFolder, fileNameIn)

                logging.info("\n---------------------------------------")
                logging.info("moving to next image")
                if borderTopBtm:
                    logging.info("filepathBorder: " + filepathBorder)
                else:
                    logging.info("filePathIn: " + filePathIn)
                logging.info("---------------------------------------")

                if borderTopBtm:
                    borderImage = cv2.imread(filePathIn)
                    #border bottom
                    cv2.rectangle(borderImage, (0, 525), (1280, 720),
                                  (0, 0, 0), -1)
                    ##border top
                    cv2.rectangle(borderImage, (0, 0), (1280, 140), (0, 0, 0),
                                  -1)
                    cv2.imwrite(filepathBorder, borderImage)

                if borderTopBtm:
                    logging.info("[YOLO] Processing %s..." % filepathBorder)
                    detections = detector.detectObjectsFromImage(
                        imageIn=filepathBorder,
                        imageOut=filePathSpare,
                        minProbPerc=imageMinProb)
                else:
                    logging.info("[YOLO] Processing %s..." % filePathIn)
                    detections = detector.detectObjectsFromImage(
                        imageIn=filePathIn,
                        imageOut=filePathSpare,
                        minProbPerc=imageMinProb)

                probPercBest = 0
                closestName = None
                bestBB = None

                img = cv2.imread(filePathIn)

                for detection in detections:
                    name = detection["name"]
                    probPerc = detection["probPerc"]
                    bbPoints = detection["bbPoints"]
                    logging.info("name=%s, probPerc=%s, bbPoints=%s" %
                                 (name, probPerc, bbPoints))

                    if probPerc > probPercBest:
                        probPercBest = probPerc
                        closestName = name
                        bestBB = bbPoints

                if bestBB is None:
                    logging.info("Nothing is detected, skipping image...")
                    shutil.copy2(filePathIn, filePathOut)
                    shutil.copy2(filePathIn, filePathStorage)
                    continue

                if probPercBest >= imageMaxProb:
                    logging.info("Model is sure that closestName is " +
                                 closestName)
                    logging.info("fileNameIn: %s" % fileNameIn)
                    logging.info("fileNameIn.split('_'): %s" %
                                 fileNameIn.split("_"))

                    robotCurrentX, robotCurrentY, robotCurrentDirection = fileNameIn.split(
                        "-")[1].split("_")[0], fileNameIn.split("-")[1].split(
                            "_")[1], fileNameIn.split("-")[1].split(
                                "_")[2].split('.')[0]
                    exportImageAbs(img, filePathOut, filePathStorage, bestBB,
                                   closestName, robotCurrentX, robotCurrentY,
                                   robotCurrentDirection)
                    continue
                else:
                    logging.warn("Model is not confident")
                    shutil.copy2(filePathIn, filePathOut)
                    shutil.copy2(filePathIn, filePathStorage)
                    continue

            except Exception as e:
                logging.error("\nException %s\n" % e)
                traceback.print_exc()

                if "!_img.empty()" in str(e):
                    logging.error("Model does not detect images ")
                    if os.path.exists(filePathIn):
                        shutil.copy2(filePathIn, filePathOut)
                        shutil.copy2(filePathIn, filePathStorage)
                    continue