def flaw_detection()

in scenarios/acsa_fault_detection/main.py [0:0]


def flaw_detection(base_dir = acsa_storage):
    """
    Measurement and defects such as color, crack and orientation of the object
    are found.

    :return: None
    """
    global HEIGHT_OF_OBJ
    global WIDTH_OF_OBJ
    global COUNT_OBJECT
    global OBJ_DEFECT
    global FRAME_COUNT
    global OBJECT_COUNT

    while cap.isOpened():
        # Read the frame from the stream
        ret, frame = cap.read()


        if not ret:
            break

        FRAME_COUNT += 1

        # Check every given frame number
        # (Number chosen based on the frequency of object on conveyor belt)
        if FRAME_COUNT % frame_number == 0:
            HEIGHT_OF_OBJ = 0
            WIDTH_OF_OBJ = 0
            OBJ_DEFECT = []
            data_base = []
            # Convert BGR image to HSV color space
            img_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

            # Thresholding of an Image in a color range
            img_threshold = cv2.inRange(img_hsv, (LOW_H, LOW_S, LOW_V),
                                        (HIGH_H, HIGH_S, HIGH_V))

            # Morphological opening(remove small objects from the foreground)
            img_threshold = cv2.erode(img_threshold,
                                      cv2.getStructuringElement(
                                          cv2.MORPH_ELLIPSE, (5, 5)))
            img_threshold = cv2.dilate(img_threshold,
                                       cv2.getStructuringElement(
                                           cv2.MORPH_ELLIPSE, (5, 5)))

            # Morphological closing(fill small holes in the foreground)
            img_threshold = cv2.dilate(img_threshold,
                                       cv2.getStructuringElement(
                                           cv2.MORPH_ELLIPSE, (5, 5)))
            img_threshold = cv2.erode(img_threshold,
                                      cv2.getStructuringElement(
                                          cv2.MORPH_ELLIPSE, (5, 5)))

            # Find the contours on the image
            contours, hierarchy = cv2.findContours(img_threshold,
                                                   cv2.RETR_LIST,
                                                   cv2.CHAIN_APPROX_NONE)

            for cnt in contours:
                x, y, w, h = cv2.boundingRect(cnt)
                if OBJECT_AREA_MAX > w * h > OBJECT_AREA_MIN:
                    box = cv2.minAreaRect(cnt)
                    box = cv2.boxPoints(box)
                    height, width = dimensions(np.array(box, dtype='int'))
                    HEIGHT_OF_OBJ = round(height * one_pixel_length * 10, 2)
                    WIDTH_OF_OBJ = round(width * one_pixel_length * 10, 2)
                    COUNT_OBJECT += 1
                    OBJECT_COUNT = "Object Number : {}".format(COUNT_OBJECT)

                    # Check for the orientation of the object
                    frame, orientation_flag, orientation_defect = \
                        detect_orientation(frame, cnt)
                    if orientation_flag:
                        value = 1
                        data_base.append(value)
                        OBJ_DEFECT.append(str(orientation_defect))
                    else:
                        value = 0
                        data_base.append(value)

                    # Check for the color defect of the object
                    frame, color_flag, color_defect = detect_color(frame, cnt)
                    if color_flag:
                        value = 1
                        data_base.append(value)
                        OBJ_DEFECT.append(str(color_defect))
                    else:
                        value = 0
                        data_base.append(value)

                    # Check for the crack defect of the object
                    frame, crack_flag, crack_defect = detect_crack(frame, cnt)
                    if crack_flag:
                        value = 1
                        data_base.append(value)
                        OBJ_DEFECT.append(str(crack_defect))
                    else:
                        value = 0
                        data_base.append(value)

                    # Check if none of the defect is found
                    if not OBJ_DEFECT:
                        value = 1
                        data_base.append(value)
                        defect = "No Defect"
                        OBJ_DEFECT.append(defect)
                        print("No defect detected in object {}"
                              .format(COUNT_OBJECT))
                        cv2.putText(frame, "Length (mm): {}".format(HEIGHT_OF_OBJ),
                                    (5, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75,
                                    (255, 255, 255), 2)
                        cv2.putText(frame, "Width (mm): {}".format(WIDTH_OF_OBJ),
                                    (5, 110), cv2.FONT_HERSHEY_SIMPLEX, 0.75,
                                    (255, 255, 255), 2)
                        cv2.imwrite("{}/Nodefect_{}.png".format(
                            base_dir, COUNT_OBJECT),
                                    frame[y : y + h,
                                          x : x + w])
                    else:
                        value = 0
                        data_base.append(value)
                    print("Length (mm) = {}, width (mm) = {}".format(
                        HEIGHT_OF_OBJ, WIDTH_OF_OBJ))
            if not OBJ_DEFECT:
                continue

        all_defects = " ".join(OBJ_DEFECT)
        cv2.putText(frame, "Press q to quit", (410, 50),
                    cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2)
        cv2.putText(frame, OBJECT_COUNT, (5, 50), cv2.FONT_HERSHEY_SIMPLEX,
                    0.75, (255, 255, 255), 2)
        cv2.putText(frame, "Defect: {}".format(all_defects), (5, 140),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 255, 255), 2)
        cv2.putText(frame, "Length (mm): {}".format(HEIGHT_OF_OBJ), (5, 80),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 255, 255), 2)
        cv2.putText(frame, "Width (mm): {}".format(WIDTH_OF_OBJ), (5, 110),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 255, 255), 2)

        ret, buffer = cv2.imencode('.png', frame)
        frame=buffer.tobytes()

        yield (b'--frame\r\n'
              b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')

    cv2.destroyAllWindows()
    cap.release()