Пример #1
0
def detect_faces(image, ind):
    THRESHOLD = 0.2

    # load our serialized model from disk
    print("[INFO] loading model...")
    net = cv2.dnn.readNetFromCaffe(
        '/model/face_detection/deploy.prototxt.txt',
        '/model/face_detection/res10_300x300_ssd_iter_140000.caffemodel')

    # load the input image and construct an input blob for the image
    # by resizing to a fixed 300x300 pixels and then normalizing it
    (h, w) = image.shape[:2]
    blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0,
                                 (300, 300), (104.0, 177.0, 123.0))

    # pass the blob through the network and obtain the detections and
    # predictions
    print("[INFO] computing object detections...")
    net.setInput(blob)
    detections = net.forward()
    count = 0

    # loop over the detections
    for i in range(0, detections.shape[2]):
        # extract the confidence (i.e., probability) associated with the
        # prediction
        confidence = detections[0, 0, i, 2]

        # filter out weak detections by ensuring the `confidence` is
        # greater than the minimum confidence
        if confidence > THRESHOLD:
            count += 1
            # compute the (x, y)-coordinates of the bounding box for the
            # object
            box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
            (startX, startY, endX, endY) = box.astype("int")

            # draw the bounding box of the face along with the associated
            # probability
            text = "{:.2f}%".format(confidence * 100) + 'Count ' + str(count)
            y = startY - 10 if startY - 10 > 10 else startY + 10
            cv2.rectangle(image, (startX, startY), (endX, endY), (0, 0, 255),
                          2)
            cv2.putText(image, text, (startX, y), cv2.FONT_HERSHEY_SIMPLEX,
                        0.45, (0, 0, 255), 2)

    cv2.imwrite('image{}.jpg'.format(ind), image)
    logger.debug('Count: {}'.format(count))
Пример #2
0
 def post(self):
     """Video detection"""
     video = request.files.getlist('video', None)
     rotation = int(request.form.get('rotation', 0))
     prob_detection = int(request.form.get('probDetection', 0))
     prob_classification = int(request.form.get('probClassification', 0))
     fps = int(request.form.get('fps', 0))
     logger.debug(video)
     logger.debug(rotation)
     if video:
         video[0].save("/tmp/video")
         task = long_task.delay(video[0].filename, rotation, prob_detection,
                                prob_classification, fps)
         return {'task_id': task.id}, 202
     else:
         return {'status': 'no video'}, 404
Пример #3
0
def long_task(self, video_name, rotation90, prob_detection,
              prob_classification, selected_fps):
    logger.debug(video_name)
    res = dict()
    cap = cv2.VideoCapture("/tmp/video", )
    media_info = MediaInfo.parse('/tmp/video')
    myjson = json.loads(media_info.to_json())
    rotation = myjson['tracks'][1]['rotation']
    total_rotation = int(float(rotation) / 90) + int(rotation90 / 90)
    logger.debug('Rotation total {}'.format(rotation))
    while not cap.isOpened():
        cap = cv2.VideoCapture("/tmp/video", )
        cv2.waitKey(1000)
        logger.debug("Wait for the header")

    pos_frame = cap.get(cv2.cv2.CAP_PROP_POS_FRAMES)
    total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    fps = int(cap.get(cv2.CAP_PROP_FPS))
    skip_images = int(fps / selected_fps)
    logger.debug(
        "Real fps {}, selected fps: {}, taking 1 image between {}".format(
            fps, selected_fps, skip_images))
    while True:
        flag, frame = cap.read()
        if flag:
            # The frame is ready and already captured
            pos_frame = int(cap.get(cv2.cv2.CAP_PROP_POS_FRAMES))
            # Every 10 frames
            if pos_frame % skip_images == 0:
                h, w, _ = frame.shape
                # frame = frame[0:h,int(2*w/3):w]
                frame = frame[0:h, 0:w]
                frame = rotate_frame90(frame, total_rotation)
                self.update_state(state='PROGRESS',
                                  meta={
                                      'current':
                                      pos_frame,
                                      'total':
                                      total_frames,
                                      'partial_result': [{
                                          'frame':
                                          res[key]['frame'],
                                          'seconds':
                                          res[key]['seconds'],
                                          'model':
                                          key,
                                          'img':
                                          res[key]['img']
                                      } for key in res]
                                  })

                output = predict_class(frame)
                if len(output) > 0:
                    for box in output:
                        logger.debug('Frame {}'.format(pos_frame))
                        logger.debug(box)
                        if float(box['confidence']) > (
                                prob_detection / 100) and float(
                                    box['prob'][0]) > (prob_classification /
                                                       100):
                            logger.debug(box['pred'][0])
                            # Print detected boxes
                            cv2.rectangle(frame, (box['x1'], box['y1']),
                                          (box['x2'], box['y2']), (255, 0, 0),
                                          6)
                            cv2.putText(frame, box['label'],
                                        (box['x1'], box['y1'] - 5),
                                        cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                                        (0, 255, 0), 2)
                            # Convert captured image to JPG
                            retval, buffer = cv2.imencode('.jpg', frame)
                            # Convert to base64 encoding and show start of data
                            jpg_as_text = base64.b64encode(buffer)
                            base64_string = jpg_as_text.decode('utf-8')
                            modele = box['pred'][0]
                            res[modele] = {
                                'frame': pos_frame,
                                'seconds': pos_frame / fps,
                                'model': box['pred'][0],
                                'img': base64_string
                            }
        else:
            break
    return {
        'current':
        total_frames,
        'total':
        total_frames,
        'status':
        'Task completed!',
        'partial_result': [{
            'frame': res[key]['frame'],
            'seconds': res[key]['seconds'],
            'model': key,
            'img': res[key]['img']
        } for key in res],
        'result':
        list(res.keys())
    }
Пример #4
0
import os
import cv2
import numpy as np
import pandas as pd
import torch
import mmcv
from mmdet.models import build_detector
from mmdet.apis import inference_detector, init_detector
from mmdet.core import get_classes

from typing import List
from matchvec.utils import timeit, logger

DETECTION_MODEL = os.getenv('DETECTION_MODEL')
DETECTION_THRESHOLD = float(os.getenv('DETECTION_THRESHOLD'))
logger.debug(DETECTION_MODEL)
logger.debug(DETECTION_THRESHOLD)

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
logger.debug(device)

# Model checkpoint at https://github.com/open-mmlab/mmdetection/blob/master/MODEL_ZOO.md
modele = dict(conf="cascade_rcnn_x101_64x4d_fpn_1x",
              checkpoint="cascade_rcnn_x101_64x4d_fpn_1x_20181218-e2dc376a")

class_to_keep = [
    'person', 'bicycle', 'car', 'motorcycle', 'bus', 'truck', 'traffic_light',
    'stop_sign', 'parking_meter', 'bench'
]