Ask Your Question
-2

how to add one varible to count different face

asked 2020-06-23 15:01:24 -0600

zakzak1994 gravatar image

updated 2020-06-23 22:29:00 -0600

deep-learning face-detection I'm working on in a project mask_face_detection using PyTorch , i want use the function cv2.putText and showing counter of all faces detected without mask , and other counter to classify all people with mask or_without_mask As you can see in the code below, this function can be classify Person with mask ,and person without mask , i need to count all people without mask using the function cv2.putText For example, show in the corner of the webcam X people wear masks with green color , and X people doesn't wear masks with red color.

this is the code :

import numpy as np
import ....
## i dont have any issue about this part 
model = load_pytorch_model('models/my_Model.pth');



id2class = {0: 'Mask', 1: 'NoMask'}


def inference(image,
              conf_thresh=0.5,
              iou_thresh=0.4,
              target_shape=(160, 160),
              draw_result=True,
              show_result=True
              ):
    '''
    Main function of detection inference
    :param image: 3D numpy array of image
    :param conf_thresh: the min threshold of classification probabity.
    :param iou_thresh: the IOU threshold of NMS
    :param target_shape: the model input size.
    :param draw_result: whether to daw bounding box to the image.
    :param show_result: whether to display the image.
    :return:
    '''

    output_info = []
    height, width, _ = image.shape
    image_resized = cv2.resize(image, target_shape)
    image_np = image_resized / 255.0 
    image_exp = np.expand_dims(image_np, axis=0)

    image_transposed = image_exp.transpose((0, 3, 1, 2))

    y_bboxes_output, y_cls_output = pytorch_inference(model, image_transposed)

    # remove the batch dimension, for batch is always 1 for inference.

    y_bboxes = decode_bbox(anchors_exp, y_bboxes_output)[0]
    y_cls = y_cls_output[0]
    # To speed up, do single class NMS, not multiple classes NMS.
    bbox_max_scores = np.max(y_cls, axis=1)
    bbox_max_score_classes = np.argmax(y_cls, axis=1)

    # keep_idx is the alive bounding box after nms.
    keep_idxs = single_class_non_max_suppression(y_bboxes,
                                                 bbox_max_scores,
                                                 conf_thresh=conf_thresh,
                                                 iou_thresh=iou_thresh,

after this part of code i need to add a counter to increment after detection a persone without mask

    for idx in keep_idxs:
            conf = float(bbox_max_scores[idx])
            class_id = bbox_max_score_classes[idx]
            bbox = y_bboxes[idx]
            # clip the coordinate, avoid the value exceed the image boundary.
            xmin = max(0, int(bbox[0] * width))
            ymin = max(0, int(bbox[1] * height))
            xmax = min(int(bbox[2] * width), width)
            ymax = min(int(bbox[3] * height), height)

            if draw_result:

================================== i need to add aa counter  in this part===================

                if class_id == 0:
                    color = (0, 255, 0)
                else:
                    color = (255, 0, 0)
                cv2.rectangle(image, (xmin, ymin), (xmax, ymax), color, 2)
                cv2.putText(image, "%s: %.2f" % (id2class[class_id], conf), (xmin + 2, ymin 

                - 2),cv2.FONT_HERSHEY_SIMPLEX, 0.8, color)
                output_info.append([class_id, conf, xmin, ymin, xmax, ymax])

    if show_result:
        Image.fromarray(image).show()
    return output_info


def run_on_video(video_path, output_video_name, conf_thresh):
    cap = cv2.VideoCapture(video_path)
    height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
    width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
    fps = cap.get(cv2.CAP_PROP_FPS)
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    # writer = cv2.VideoWriter(output_video_name, fourcc, int(fps), (int(width), int(height)))
    total_frames = cap.get(cv2.CAP_PROP_FRAME_COUNT)
    if not cap.isOpened():
        raise ValueError("Video open failed.")
        return
    status = True
    idx = 0
    while status:
        start_stamp = time.time()
        status, img_raw = cap.read()
        img_raw = cv2.cvtColor(img_raw, cv2.COLOR_BGR2RGB)
        read_frame_stamp = time.time()
        if (status):
            inference(img_raw,
                      conf_thresh,
                      iou_thresh ...
(more)
edit retag flag offensive close merge delete

Comments

How do you called function run_on_video. I don't see.

supra56 gravatar imagesupra56 ( 2020-06-23 21:18:11 -0600 )edit

Still got confused, because indentation isn't properly.:

================================== i need to add aa counter  in this part===================

                    if class_id == 0:
                    color = (0, 255, 0)
                else:
                    color = (255, 0, 0)
                cv2.rectangle(image, (xmin, ymin), (xmax, ymax), color, 2)
                cv2.putText(image, "%s: %.2f" % (id2class[class_id], conf), (xmin + 2, ymin 

- 2),
supra56 gravatar imagesupra56 ( 2020-06-23 21:23:50 -0600 )edit

@supra56 please check the code again , i edited it .

zakzak1994 gravatar imagezakzak1994 ( 2020-06-23 22:22:42 -0600 )edit
1

@supra56 i corrected indentation please check it again ,thank you

zakzak1994 gravatar imagezakzak1994 ( 2020-06-23 22:30:02 -0600 )edit

you obviously did not "think this though" to the end.

to achieve what you want, you'd also need tracking (to avoid counting ppl twice) and maybe even a coarse face recognition

berak gravatar imageberak ( 2020-06-24 01:37:09 -0600 )edit

@berak please give me the answer Although there is conting ppl twice

zakzak1994 gravatar imagezakzak1994 ( 2020-06-24 02:19:26 -0600 )edit
1

sorry, but i won't write your program.

you'll have to do some research on your own, for a change, not just copypaste other ppls code, and then ask why it does not work

berak gravatar imageberak ( 2020-06-24 02:28:11 -0600 )edit

Ok thanks Sir , for your information im just biginner and i cant write my own code in this moment , At this point, I am trying to understand and learning different codes opensource , and add some lines to it , when i will become expert , i dont need open cases in any platform.

zakzak1994 gravatar imagezakzak1994 ( 2020-06-24 02:44:42 -0600 )edit

Your code is too big. You should try haarcascade_frontalface.xml instead of deep learning. It should be 60 lines.

supra56 gravatar imagesupra56 ( 2020-06-24 04:37:33 -0600 )edit
2

@supra56 i already tested haascascade_frontalface.xml it's does not give good result for detection faces , that's why i choose deep learning. Thank you for your reply.

zakzak1994 gravatar imagezakzak1994 ( 2020-06-24 05:18:44 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-06-24 06:06:08 -0600

supra56 gravatar image

updated 2020-06-26 03:41:09 -0600

Sorry. I had this on my bookmark about 3 weeks ago. This link doesn't used pytorch. Here is link: covid-19 mask Another one face mask And this too face mask 2

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-06-23 15:01:24 -0600

Seen: 530 times

Last updated: Jun 26 '20