<jupyter_start><jupyter_text>Day 1 Face Detection with OpenCV and Deep Learning> Day 1 Face Detection with OpenCV and Deep Learning- toc: true - badges: true- comments: true- categories: [OpenCV]- image: images/chart-preview.png<jupyter_code>#collapse-hide
# Convert jupyter notebook to python script:
#!jupyter nbconvert --to script file-name.ipynb
#$ python file-name.py --image01 image01.png  etc
# in ap.add_arrgument use : required = True + remove default path
#args = vars(ap.parse_args())
import numpy as np
import argparse
import cv2
import os
import matplotlib.pyplot as plt
# construct the argument parse and parse the arguments
#ap.add_argument('--name', '-n', default='foo', help='foo')
ap = argparse.ArgumentParser(description='Fooo')
ap.add_argument("-i", "--image",
                default = "images/face_detection05.jpg", required=False,# for *.py use required= True
    help="path to input image")
ap.add_argument("-p", "--prototxt", default = "models/deploy.prototxt.txt",
    help="path to Caffe 'deploy' prototxt file")
ap.add_argument("-m", "--model", default = "models/res10_300x300_ssd_iter_140000.caffemodel",#required=True,
    help="path to Caffe pre-trained model")
ap.add_argument("-c", "--confidence", type=float, default=0.90,
    help="minimum probability to filter weak detections")
args = vars(ap.parse_args([])) #for *.py use args = vars(ap.parse_args())
# load the input image and construct an input blob for the image
# by resizing to a fixed 300x300 pixels and then normalizing it
image = cv2.imread(args["image"])
(h, w) = image.shape[:2]
blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0))
print(h, w)
print("[INFO] loading model...")
net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"])
print("...done")
# pass the blob through the network and obtain the detections and
# predictions
print("[INFO] computing object detections...")
net.setInput(blob)
detections = net.forward()
print("...done")
# 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 > args["confidence"]:
        # 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)
        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)

# show the output image
plt.imshow(image)
cv2.imwrite("images/face_detection05_t09.jpg", image)
#cv2.waitKey(0)<jupyter_output><empty_output><jupyter_text>Tune model by varying threshold for edge detection.<jupyter_code>fig = plt.figure(figsize = (10,10))
image_th02 = "images/face_detection05_t02.jpg"
#image_th05 = "face_detection05_t05.jpg"
image_th09 = "images/face_detection05_t09.jpg"
image_th02 = cv2.imread(image_th02)
#image_th05 = "face_detection05_t05.jpg"
image_th09 = cv2.imread(image_th09)
#print(image)
images = ("th = 0.2", image_th02), ("th = 0.9", image_th09)
# loop over the images
for (i, (name, image)) in enumerate(images):
    # show the image
    ax = fig.add_subplot(1, 2, i + 1)
    ax.set_title(name)
    plt.imshow(image)
    plt.axis("off")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14060411654637797, 'fraction_numerical': 0.04223469660518578, 'mean_word_length': 4.147180192572215, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '355901', 'license': 'Apache-2.0', 'n_tokens_mistral': 1396, 'n_tokens_neox': 1267, 'n_words': 366, 'path': '_notebooks/2020-12-22-Day1_Face_Detection_OpenCV.ipynb', 'repo_name': 'simsisim/sims-pyimage', 'size': '3741'}