示例#1
0
def main(image_file):
    detector = Detector(weight_dir=MODEL_WEIGHT_SAVE_DIR,
                        mode=3,
                        min_face_size=24)
    input_img_full_path = TEST_INPUT_IMG_DIR + '/' + image_file
    output_img_full_path = TEST_OUTPUT_IMG_DIR + '/' + image_file
    image = cv2.imread(input_img_full_path)
    bbox, bboxes, landmarks = detector.predict(image)

    print('bboxes-shape---:', bboxes.shape)
    print('landmarks-shape---:', landmarks.shape)
    for bbox in bboxes:
        #print('bbox score--:',bbox[4])
        #cv2.putText(image,str(np.round(bbox[4],2)),(int(bbox[0]),int(bbox[1])),cv2.FONT_HERSHEY_TRIPLEX,1,color=(255,0,255))
        cv2.rectangle(image, (int(bbox[0]), int(bbox[1])),
                      (int(bbox[2]), int(bbox[3])), (0, 0, 255))

    for landmark in landmarks:
        #print('landmark-shape---:',landmark.shape)
        #print('landmark----:',landmark)

        for i in range(0, 5):
            cv2.circle(image,
                       (int(landmark[2 * i]), int(int(landmark[2 * i + 1]))),
                       3, (0, 0, 255))
        #break

    cv2.imwrite(output_img_full_path, image)
    cv2.imshow('yy', image)
    cv2.waitKey(0)
示例#2
0
def main():
    print("Running Task")
    camera = Camera()
    d = Detector()
    rs = d.detect_image(camera.capture())
    print(rs)
    for obj, confidence in rs.items():
        print('Object {} : Confidence {}'.format(obj, confidence))
示例#3
0
 def __init__(self):
     Thread.__init__(self)
     self.camera = Camera()
     self.detector = Detector()
     self.stop_loop = False
     self.telemetry = Telemetry()
     self.position_calculator = PositionCalculator()
     self.frame = None
示例#4
0
def detection(d):
    det = Detector(detector_name='mobilenet_ssd', config_path='./detectors.cfg')
    while True:
        if(d['tracker_ready'] == True):
            results = det.detect_image_frame(d['%d_frame'%d['index']], to_xywh=True)
            d['%d_boxes'%d['index']] = np.array([result[1:5] for result in results])
            d['%d_scores'%d['index']] = np.array([result[5] for result in results])
            d['tracker_ready'] = False
            d['detector_ready'] = True
示例#5
0
def detect(cluster, id):
    img_catalog = make_camera_img_catalog(cluster, id)
    predictor = Detector(config_path='detector/configs/faster_rcnn_R_101_FPN_3x.yaml',
                         weight_path='detector/configs/faster_rcnn_R_101.pkl')
    img_catalog = add_static_to_catalog(img_catalog)
    bboxes_catalog = 'static/img/cameras/cluster_' + str(cluster) + '/camera_' + str(id) + '/detected'
    clean_directory(bboxes_catalog)
    camera_cluster_id = str(cluster) + str(id)
    predictor.save_bboxes(img_catalog, camera_cluster_id)
    bboxes = make_camera_detected_img_catalog(cluster, id)
    return render_template('detected_images.html', image_catalog=bboxes, cluster=cluster, id=id)
def load_model():
    #
    cropper = Cropper()
    #
    detector = Detector(config.DETECTOR_CFG, config.DETECTOR_WEIGHT, config.DETECTOR_LABELS)
    #
    reader_config = Cfg.load_config_from_file(config.READER_CFG)
    reader_config['weights'] = config.READER_WEIGHT
    reader_config['device'] = config.DEVICE
    reader = Reader(reader_config)
    #
    return cropper, detector, reader
示例#7
0
    def test_detector_image(self):
        # Create Localizer
        cfg_path = './data/yolo/full/trafficsigns.cfg'
        weights_path = './data/yolo/full/trafficsigns.weights'
        threshold = 0.24

        localizer = Localizer(cfg_path, weights_path, threshold, gpu=0.0)

        # Create cropper
        crop_percent = 0.25
        force_square = True

        cropper = Cropper(crop_percent, force_square)

        # Create classifier
        model_path = './data/classifier/trafficsigns.json'
        weights_path = './data/classifier/trafficsigns.h5'
        labels_path = './data/classifier/classes.txt'
        threshold = 0.5

        classifier = Classifier(model_path, weights_path, labels_path,
                                threshold)

        # Create detection pipeline
        detection_pipeline = DetectionPipeline(localizer, cropper, classifier)

        # Create detector
        images_path = './data/classifier/classes/'
        sounds_path = './data/sounds/'

        detector = Detector(detection_pipeline, images_path, sounds_path)

        # Detect on image
        img = cv2.imread('./tests/data/test.png')

        image, detections = detector.detect_image(img,
                                                  show_confidence=True,
                                                  return_image=True)

        true_detections = [{
            'class_id': 15,
            'coordinates': [1434, 456, 1590, 612],
            'label': 'max-60',
            'confidence': 1.0
        }]

        assert detections == true_detections
示例#8
0
    def test_detector_video(self):
        # Create Localizer
        cfg_path = './data/yolo/full/trafficsigns.cfg'
        weights_path = './data/yolo/full/trafficsigns.weights'
        threshold = 0.24

        localizer = Localizer(cfg_path, weights_path, threshold, gpu=0.0)

        # Create cropper
        crop_percent = 0.25
        force_square = True

        cropper = Cropper(crop_percent, force_square)

        # Create classifier
        model_path = './data/classifier/trafficsigns.json'
        weights_path = './data/classifier/trafficsigns.h5'
        labels_path = './data/classifier/classes.txt'
        threshold = 0.5

        classifier = Classifier(model_path, weights_path, labels_path,
                                threshold)

        # Create detection pipeline
        detection_pipeline = DetectionPipeline(localizer, cropper, classifier)

        # Create detector
        images_path = './data/classifier/classes/'
        sounds_path = './data/sounds/'

        detector = Detector(detection_pipeline, images_path, sounds_path)

        # Detect on video
        video_feed = './tests/data/test.mp4'

        output_mp4 = './tests/data/test_output.mp4'
        output_csv = './tests/data/test_output.csv'

        exit_code = detector.detect_video_feed(video_feed,
                                               output=output_mp4,
                                               output_csv=output_csv,
                                               show_confidence=True,
                                               sound_notifications=True)

        assert exit_code == True
示例#9
0
async def main():
    log_levels = ['CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG']
    config_template = {
        'devices': confuse.Sequence(BluetoothDeviceConfuseTemplate(), ),
        'log_level': confuse.Choice(log_levels, default=DEFAULT_LOG_LEVEL),
        'mqtt': {
            'enabled':
            confuse.Choice([True, False], default=DEFAULT_MQTT_ENABLED),
            'host':
            confuse.String(default=DEFAULT_MQTT_HOST),
            'log_level':
            confuse.Choice(['ERROR', 'WARNING', 'INFO', 'DEBUG'],
                           default=DEFAULT_MQTT_LOG_LEVEL),
            'port':
            confuse.Integer(default=DEFAULT_MQTT_PORT),
            'protocol':
            confuse.String(default=DEFAULT_MQTT_PROTOCOL),
        },
        'scheduler': {
            'log_level':
            confuse.Choice(log_levels, default=DEFAULT_SCHEDULER_LOG_LEVEL),
        },
    }
    config = confuse.Configuration('bluetooth_tracker',
                                   __name__).get(config_template)

    kwargs = {}
    kwargs['bluetooth_rssi_scanner'] = FakeBluetoothScanner
    kwargs['bluetooth_lookup_name_func'] = lambda *_: 'test'
    detector = Detector(
        config,
        mqtt.Client(),
        AsyncIOScheduler(),
        **kwargs,
    )
    detector.start_detecting()

    killer = GracefulKiller()
    while not killer.kill_now:
        await asyncio.sleep(1)

    logging.info('shutting down')
    detector.stop_detecting()
示例#10
0
def yolo():
    image = cv2.imread(args["image"])
    weightsPath = get_yolo_path("yolov3.weights")
    configPath = get_yolo_path("yolov3.cfg")
    labelsPath = get_yolo_path("coco.names")
    LABELS = open(labelsPath).read().strip().split("\n")
    np.random.seed(42)
    COLORS = np.random.randint(0, 255, size=(len(LABELS), 3), dtype="uint8")

    # load our YOLO object detector trained on COCO dataset (80 classes)
    print("[INFO] loading YOLO from disk...")
    net = cv2.dnn.readNetFromDarknet(configPath, weightsPath)

    detector = Detector(net, image, args['confidence'], args['threshold'],
                        LABELS, COLORS)
    img = detector.detect()

    cv2.imshow("Image", img)
    cv2.waitKey(0)
def callback(data):
    bridge = CvBridge()
    height, width = 1024, 1280

    detector = Detector(height=height, width=width, is_live=False)
    try:
        image = bridge.imgmsg_to_cv2(data, "bgr8")
    except CvBridgeError as e:
        print(e)
    data = detector.detection_by_frame(image)
    print('parking center found =  ', data['found'])
    #cv2.imwrite('/home/artemon12/PycharmProjects/parking_diploma/pics/lot_res1.jpg', image)
    out.write(image)

    if data['found']:
        quat = quaternion_from_euler(0, 0, np.pi / 2)

        x_posititon = 0.7188 + data['parking_params']['x']
        y_posititon = -1 * (0.7967 + data['parking_params']['y'])

        position = PoseStamped()
        position.header.seq = 0
        position.header.stamp = rospy.Time.now()
        position.header.frame_id = 'map'
        position.pose.position = Point(x_posititon, y_posititon, 0)
        position.pose.orientation = Quaternion(*quat)

    if data['found'] and TRIGGER is not True:
        global TIME
        if TIME is 0:
            TIME = data['time']

        POINTS.append(data['parking_params']['center'])

        if data['time'] - TIME > 1.5:
            global TRIGGER
            global POINTS
            average = np.std(POINTS)
            if average < 5:
                parking_pub.publish(position)
                TRIGGER = True
示例#12
0
    def test_detector_image_output_json(self):
        # Create Localizer
        cfg_path = './data/yolo/full/trafficsigns.cfg'
        weights_path = './data/yolo/full/trafficsigns.weights'
        threshold = 0.24

        localizer = Localizer(cfg_path, weights_path, threshold, gpu=0.0)

        # Create cropper
        crop_percent = 0.25
        force_square = True

        cropper = Cropper(crop_percent, force_square)

        # Create classifier
        model_path = './data/classifier/trafficsigns.json'
        weights_path = './data/classifier/trafficsigns.h5'
        labels_path = './data/classifier/classes.txt'
        threshold = 0.5

        classifier = Classifier(model_path, weights_path, labels_path,
                                threshold)

        # Create detection pipeline
        detection_pipeline = DetectionPipeline(localizer, cropper, classifier)

        # Create detector
        images_path = './data/classifier/classes/'
        sounds_path = './data/sounds/'

        detector = Detector(detection_pipeline, images_path, sounds_path)

        # Detect on image
        img = cv2.imread('./tests/data/test.png')

        output_filename = './tests/data/detector_image.json'
        detections = detector.detect_image(img, output=output_filename)

        assert os.path.exists(output_filename) == True
def main(input_net_name):
    net_name = input_net_name
    assert net_name in NET_NAMES
    images_dir = WIDER_FACE_IMG_DIR
    annotation_file = WIDER_FACE_ANNO_FILE
    out_dir = GAN_DATA_ROOT_DIR
    out_dir = '{}/{}'.format(out_dir, net_name)
    
    if net_name == 'r_net':
        mode = 1
    elif net_name == 'o_net':
        mode = 2
    
    #images_path images_jpg bboxes
    dataset = load_widerface_dataset(images_dir, annotation_file)

    detector = Detector(weight_dir= MODEL_WEIGHT_SAVE_DIR, mode=mode)
    
    bboxes_all = []
    
    #p_net 一次测一张图片,注意,其返回可能会有多个,因为图片中可以包含多张面,而且还有图片金字塔
    print('data img len --:',len(dataset['images_jpg']))
    for img_jpg in dataset['images_jpg']:
        _, bboxes, _ = detector.predict(img_jpg)
        bboxes_all.append(bboxes)
    
    bboxes_all = np.array(bboxes_all)
    print('predict over---')
    if not os.path.exists(out_dir):
        os.mkdir(out_dir)
    
    detections_path = os.path.join(out_dir, 'middle_wideface_data_det.pkl')
    with open(detections_path, 'wb') as f:
        pickle.dump({
            'bboxes': bboxes_all,
        }, f)
    
    save_hard_examples(net_name, out_dir, dataset, detections_path)
示例#14
0
文件: pipeline.py 项目: dqkqd/aihcmc
    def __init__(self, args):

        self.args = args
        self.num_classes = 4

        self.device = 'cuda' if torch.cuda.is_available() else 'cpu'
        assert self.device == 'cuda', 'CUDA is not available'
        images_folder = os.path.join(args.images_root, args.videoname)
        self.dataset = Vehicle(images_folder,
                               model=args.model_type,
                               image_size_effdet=args.image_size_effdet)
        self.loader = DataLoader(self.dataset,
                                 batch_size=args.batch_size,
                                 shuffle=False,
                                 sampler=SequentialSampler(self.dataset),
                                 num_workers=args.num_workers,
                                 pin_memory=False,
                                 drop_last=False)

        self.detector = Detector(args.weight, args.conf_thres, args.iou_thres,
                                 args.model_type, args.image_size_effdet)

        self.tracker = JDETracker(max_age=args.max_age,
                                  buffer_size=args.buffer_size,
                                  det_thresh=args.conf_thres,
                                  thresh1=args.tracker_thresh1,
                                  thresh2=args.tracker_thresh2,
                                  thresh3=args.tracker_thresh3)
        self.track_features_type = args.track_features_type

        self.save_detects = args.save_detects
        self.detect_outputs = args.detect_outputs
        self.detect_results = []

        self.track_outputs = args.track_outputs
        self.track_results = []
示例#15
0
from detector.detector import Detector

if __name__ == '__main__':
    det = Detector(detector_name='yolo', config_path='config.cfg')

    # detect_image_test
    #det.detect_image('./_samples/MOT17-09-FRCNN/img1/000055.jpg')

    # detect_video_test
    #det.detect_video('./_samples/MOT17-09-FRCNN.mp4')

    # detect_webcam_test
    #det.detect_webcam()

    #det = Detector(detector_name='mobilenet', config_path='detectors.cfg')
    #det.detect_webcam()

    # Detect image sequence
示例#16
0
from rpi import rec_utils
from rpi import pi_utils
import utils.audio_utils as au
from utils.state_machine import StateMachine

from detector.detector import Detector
import pickle

DIRECTORY = "records_detector"
MODEL_DIR = "models/svc.pickle"


if __name__ == "__main__":
    rec_saver = au.RecSaver(DIRECTORY)
    with open(MODEL_DIR, "rb") as model_file:
        detector = Detector(pickle.load(model_file))
    audio_rec = rec_utils.AudioRecorder()
    state_machine = StateMachine(audio_rec, detector, rec_saver)
    button = pi_utils.Button(4)

    while 1:
        button_pressed = button.check_value()
        if button_pressed:
            state_machine.change_state()
        state_machine.run()
示例#17
0
from telemetry.telemetry import Telemetry
from positionCalculator.positionCalculator import PositionCalculator
import cv2
from settings.settings import Values
from detector.detector import Detector
import time

if __name__ == '__main__':
    frame = cv2.imread("images/pole.png")
    telemetry = Telemetry()
    positionCalculator = PositionCalculator()
    detector = Detector()

    width, height = frame.shape[1::-1]

    altitude = 10
    x_offset = 0
    y_offset = 0
    azimuth = 0
    telemetry.update_telemetry_manually(width / 2, height / 2, azimuth,
                                        altitude)

    try:
        if Values.PRINT_FPS:
            last_time = time.time()
            ind = 0
        while True:
            frame = cv2.imread("images/pole.png")
            left = x_offset + int(width / 2 - width * altitude / 30)
            right = x_offset + int(width / 2 + width * altitude / 30)
            top = y_offset + int(height / 2 - height * altitude / 30)
示例#18
0
    def predictcmnd(self, image):

        # check cccd
        """
        =========================================
        ===== Crop and align id card image
        =========================================
        """
        request = predict_pb2.PredictRequest()
        # model_name
        request.model_spec.name = "cropper_cmnd_model"
        # signature name, default is 'serving_default'
        request.model_spec.signature_name = "serving_default"
        # preprocess image
        img, original_image, original_width, original_height = preprocess_image(
            image, Cropper.TARGET_SIZE)
        if img.ndim == 3:
            img = np.expand_dims(img, axis=0)
        # request to cropper model
        request.inputs["input_1"].CopyFrom(
            tf.make_tensor_proto(img, dtype=np.float32, shape=img.shape))
        try:
            result = self.stub.Predict(request, 10.0)
            result = result.outputs["tf_op_layer_concat_14"].float_val
            result = np.array(result).reshape((-1, 9))

        except Exception as e:
            print("Cropper cmnd = ", e)

        cropper = Cropper()
        cropper.set_best_bboxes(result,
                                original_width=original_width,
                                original_height=original_height,
                                iou_threshold=0.5)

        # respone to client if image is invalid
        if cropper.respone_client(threshold_idcard=0.8) == -1:
            # print(cropper.respone_client(threshold_idcard=0.8))
            return
        elif cropper.respone_client(threshold_idcard=0.8) == 0:
            # print("no cropper")
            # cv2.imwrite('app/static/aligned_images/' + filename, original_image)
            aligned_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)
        else:
            # print("cropper image")
            cropper.set_image(original_image=original_image)
            # print("Cropper cmnd end")
            # output of cropper part
            aligned_image = getattr(cropper, "image_output")
            cv2.imwrite('storage/c.jpg', aligned_image)
            aligned_image = cv2.cvtColor(aligned_image, cv2.COLOR_BGR2RGB)
        """
        ===========================================
        ==== Detect informations in aligned image
        ===========================================
        """
        # preprocess aligned image
        original_height, original_width, _ = aligned_image.shape
        img = cv2.resize(aligned_image, Detector.TARGET_SIZE)
        img = np.float32(img / 255.)
        # model_name
        request.model_spec.name = "detector_cmnd_model"
        # signature name, default is 'serving_default'
        request.model_spec.signature_name = "serving_default"

        if img.ndim == 3:
            img = np.expand_dims(img, axis=0)
        # new request to detector model
        request.inputs["input_1"].CopyFrom(
            tf.make_tensor_proto(img, dtype=np.float32, shape=img.shape))

        try:
            # print("Detect cmnd = ok")
            result = self.stub.Predict(request, 10.0)
            result = result.outputs["tf_op_layer_concat_14"].float_val
            result = np.array(result).reshape((-1, 13))
            # print("Detect cmnd = end")

        except Exception as e:
            print("Detect cmnd = ", e)

        detector = Detector()
        detector.set_best_bboxes(result,
                                 original_width=original_width,
                                 original_height=original_height,
                                 iou_threshold=0.5)
        detector.set_info_images(original_image=aligned_image)
        # output of detector part
        info_images = getattr(detector, "info_images")
        return info_images
示例#19
0
import base64, cv2, random
import numpy as np
from flask import Flask, request, jsonify

from detector.detector import Detector, OP_RECT, OP_KEYPOINTS

app = Flask(__name__)
det = Detector()

@app.route('/detect', methods=['POST'])
def detect():
    image_base64 = request.json['image']
    padding = request.json.get('padding')
    if padding is None:
        padding = 0
    keypoints = request.json.get('keypoints')
    if keypoints is None:
        keypoints = False
    rect = request.json.get('rect')
    if rect is None:
        rect = False
    crop = request.json.get('crop')
    if crop is None:
        crop = False
    rgb = request.json.get('rgb')
    # OpenCV works with BGR, so we reverse RGB
    rgb = rgb[::-1] if rgb else None
    print('rgb is ', rgb)

    npimg = np.fromstring(base64.b64decode(image_base64), np.uint8)
    img = cv2.imdecode(npimg, cv2.IMREAD_COLOR)
示例#20
0
 def __init__(self,
              detector_name,
              tracker_name,
              config_path='./config.cfg'):
     self.det = Detector(detector_name, config_path)
     self.tra = Tracker_temp(tracker_name, config_path)
示例#21
0
def run(opt):

    # output dir
    if os.path.exists(opt.save_dir):
        shutil.rmtree(opt.save_dir)
    os.makedirs(opt.save_dir)

    # load dataset
    dataset = Dataloader(source=opt.source, imgsz=opt.img_size).dataset

    # load object detection model, and weights
    detector = Detector(detector_type=opt.detector_type,
                        cfg_file=opt.detector_cfg_file)
    detector.run_through_once(opt.img_size)  # 空跑一次

    # load object tracking model
    tracker = Tracker(tracker_type=opt.tracker_type,
                      cfg_file=opt.tracker_cfg_file)

    # load pose detection model
    poser = Poser(poser_type=opt.poser_type, cfg_file=opt.poser_cfg_file)

    # load classifier model
    clssifier = Classifier(classifier_type=opt.classifier_type,
                           cfg_file=opt.classifier_cfg_file)

    print(detector.device, detector.cfg)
    filt_with_txt = False  # 先分析一下status标注文件.txt,存在的才进行检测,这样能加快速度
    if filt_with_txt:
        from classifier.data_analyse import anaylise_label
        label_ret = anaylise_label()
        label_stems = [x[0] for x in label_ret]

    for img_idx, (path, img, im0s, vid_cap) in enumerate(dataset):
        # print(type(img), type(im0s))
        # print(type(im0s), im0s.shape)
        if dataset.is_camera:
            im0s = im0s[0]
            path = f'{path[0]}/{img_idx:0<6}.jpg'
        if filt_with_txt:
            fold_stem = path.split('/')[-2]
            idx = label_stems.index(fold_stem)
            # print(fold_stem, label_stems, idx)
            img_stem = Path(path).stem
            valid_stems = [Path(x).stem for x in label_ret[idx][-1]]
            in_it = f'track_{img_stem}' in valid_stems
            # print(path, in_it, label_ret[idx][-1][0])
            if not in_it:
                continue
        # img: [3, w, h], preprocess, inference, NMS,
        det_ret = detector.detect(
            path, img,
            im0s)  # detect result: nparray, [num_obj, 6] 6: xyxy,conf,cls
        # detector.imshow(im0s, det_ret)
        # track
        tra_ret = tracker.track(
            det_ret,
            im0s)  # track result: list, [num_obj, 7], 7: xyxy, cls, tid, trace
        # print(tra_ret[:, 5])
        # tracker.imshow(im0s, tra_ret, path)
        # pose detect
        pose_ret = poser.detect_pose(tra_ret, im0s, path, return_type='zzd')
        # zzd format: np.array(object): [num_obj, 10],10: xyxy cls tid trace keypoints kp_score proposal_score
        # print(pose_ret)
        poser.imshow(im0s, pose_ret, path, resize=(1280, 720))
        # classifier
        if opt.feature_save_dir is not None:  # 保存特征的
            clssifier.build_and_save_feature(pose_ret,
                                             path,
                                             save_dir=opt.feature_save_dir)
            print(f'\rsaving features: [{img_idx + 1:>3}/{len(dataset)}] ',
                  end='')
            continue

        # status_ret = clssifier.detect_status(pose_ret, path, is_camera=dataset.is_camera)
        # zzd format: np.array(object): [num_obj, 12], 12: 比10多了status_idx和status
        # clssifier.imshow(im0s, status_ret, show_name='x', resize=(1280, 720))
        # print(status_ret)

        if img_idx == 10:
            if cv2.waitKeyEx(0) == ord('q'):
                raise StopIteration
示例#22
0
from camera.camera import VideoCamera
from detector.detector import Detector
import configparser
import sys

if len(sys.argv) < 2:
    print('Usage:', sys.argv[0], '<config_file>')
    print('Example:', sys.argv[0], 'config/config.cfg')
    exit(0)

app = Flask(__name__)

config = configparser.ConfigParser()
config.read(sys.argv[1])

detector = Detector(config['tflite']['model'],
                    config['tflite'].getboolean('use_edgetpu'))

labels = config['tflite']['labels']
src0 = config['cameras'].getint('src0')
threshold = config['cameras'].getfloat('threshold')


@app.route('/')
def index():
    return render_template('index.html')


def gen(camera):
    while True:
        frame = camera.get_frame()
        yield (b'--frame\r\n'
示例#23
0
def predict(filename):

    channel = grpc.insecure_channel("localhost:8500")
    stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)

    request = predict_pb2.PredictRequest()
    # model_name
    request.model_spec.name = "cropper_model"
    # signature name, default is 'serving_default'
    request.model_spec.signature_name = "serving_default"
    start = time.time()
    """
    =========================================
    ===== Crop and align id card image
    =========================================
    """
    filepath = app.config["IMAGE_UPLOADS"] + "/" + filename
    # preprocess image
    img, original_image, original_width, original_height = preprocess_image(
        filepath, Cropper.TARGET_SIZE)
    if img.ndim == 3:
        img = np.expand_dims(img, axis=0)
    # request to cropper model
    request.inputs["input_1"].CopyFrom(
        tf.make_tensor_proto(img, dtype=np.float32, shape=img.shape))
    try:
        result = stub.Predict(request, 10.0)
        result = result.outputs["tf_op_layer_concat_14"].float_val
        result = np.array(result).reshape((-1, 9))

    except Exception as e:
        print(e)

    cropper = Cropper()
    cropper.set_best_bboxes(result,
                            original_width=original_width,
                            original_height=original_height,
                            iou_threshold=0.5)

    # respone to client if image is invalid
    if not cropper.respone_client(threshold_idcard=0.8):
        return render_template('upload_image_again.html')

    cropper.set_image(original_image=original_image)

    # output of cropper part
    aligned_image = getattr(cropper, "image_output")
    cv2.imwrite('app/static/aligned_images/' + filename, aligned_image)
    aligned_image = cv2.cvtColor(aligned_image, cv2.COLOR_BGR2RGB)
    """
    ===========================================
    ==== Detect informations in aligned image
    ===========================================
    """
    # preprocess aligned image
    original_height, original_width, _ = aligned_image.shape
    img = cv2.resize(aligned_image, Detector.TARGET_SIZE)
    img = np.float32(img / 255.)
    # model_name
    request.model_spec.name = "detector_model"
    # signature name, default is 'serving_default'
    request.model_spec.signature_name = "serving_default"

    if img.ndim == 3:
        img = np.expand_dims(img, axis=0)
    # new request to detector model
    request.inputs["input_1"].CopyFrom(
        tf.make_tensor_proto(img, dtype=np.float32, shape=img.shape))

    try:
        result = stub.Predict(request, 10.0)
        result = result.outputs["tf_op_layer_concat_14"].float_val
        result = np.array(result).reshape((-1, 13))

    except Exception as e:
        print(e)

    detector = Detector()
    detector.set_best_bboxes(result,
                             original_width=original_width,
                             original_height=original_height,
                             iou_threshold=0.5)
    detector.set_info_images(original_image=aligned_image)
    # output of detector part
    info_images = getattr(detector, "info_images")
    """
    =====================================
    ==== Reader infors from infors image
    =====================================
    """
    keys = list(info_images.keys())
    keys.remove("thoi_han")
    keys.remove("chan_dung")
    infors = dict()

    # init default value of quoc_tich, dan_toc
    infors['quoc_tich'] = ""
    infors['dan_toc'] = ""

    if "quoc_tich" in keys:
        infors['quoc_tich'] = ["Việt Nam"]
        keys.remove("quoc_tich")

    if "sex" in keys:
        info_image = info_images["sex"]
        infors["sex"] = list()
        for i in range(len(info_image)):
            img = info_image[i]['image']
            s = reader.predict(img)
            if "Na" in s:
                infors["sex"].append("Nam")
            else:
                infors["sex"].append("Nữ")
        keys.remove("sex")

    if "dan_toc" in keys:
        info_image = info_images["dan_toc"]
        infors["dan_toc"] = list()
        for i in range(len(info_image)):
            img = info_image[i]['image']
            s = reader.predict(img)
            s = s.split(" ")[-1]
            infors["dan_toc"].append(s)

        keys.remove("dan_toc")

    for key in keys:
        infors[key] = list()
        info_image = info_images[key]
        for i in range(len(info_image)):
            img = info_image[i]['image']
            s = reader.predict(img)
            infors[key].append(s)
    que_quan_0 = infors['que_quan'][0]
    que_quan_1 = ''
    noi_thuong_tru_0 = infors['noi_thuong_tru'][0]
    noi_thuong_tru_1 = ''
    if len(infors['que_quan']) == 2:
        que_quan_1 = infors['que_quan'][1]
    if len(infors['noi_thuong_tru']) == 2:
        noi_thuong_tru_1 = infors['noi_thuong_tru'][1]

    print("total_time:{}".format(time.time() - start))
    return render_template('predict.html',
                           id=infors['id'][0].replace(" ", ""),
                           full_name=infors['full_name'][0],
                           date_of_birth=infors['date_of_birth'][0],
                           sex=infors['sex'][0],
                           quoc_tich=infors['quoc_tich'],
                           dan_toc=infors['dan_toc'],
                           que_quan_0=que_quan_0,
                           que_quan_1=que_quan_1,
                           noi_thuong_tru_0=noi_thuong_tru_0,
                           noi_thuong_tru_1=noi_thuong_tru_1,
                           filename=str(filename))
示例#24
0
from detector.detector import Detector
import pandas as pd

# input image
input_path = '/media/herval/Save/School/THB/Master/Semestre3/Projekt/data/processed/test/test_sub_image_x1-2692_y1-2512_x2-3320_y2-3525.jpeg'

# output folder
output_path = '/media/herval/Save/School/THB/Master/Semestre3/Projekt/data/processed/test/outputs'

# real centers
real_centers_path = '/media/herval/Save/School/THB/Master/Semestre3/Projekt/data/processed/test/test_sub_image_x1-2692_y1-2512_x2-3320_y2-3525_marked.csv'
real = pd.read_csv(real_centers_path, sep=',', header=0).values[:, 1:]

# instantiate the detector
detector = Detector(input_path, output_path)

detector.recognize()

detector.mapping(real)
from camera.threadcamera import ThreadCamera
from detector.detector import Detector
from detector.threaddetector import ThreadDetector
import signal

signal.signal(signal.SIGINT, signal.SIG_DFL)

if __name__ == '__main__':

    camera = Camera()
    print(camera)

    app = QtGui.QApplication(sys.argv)
    window = Gui(camera)

    detector = Detector(camera, window)
    print(detector)

    t_cam = ThreadCamera(camera)
    t_cam.start()

    t_detector = ThreadDetector(detector)
    t_detector.start()

    window.setCamera(camera, t_cam)
    window.show()
    window.setDetector(detector, t_detector)
    window.show()

    t_gui = ThreadGui(window)
    t_gui.start()