0% found this document useful (0 votes)
2 views

A4_exam

The document outlines a computer vision application using OpenCV to detect shapes, colors, and faces in real-time from a webcam feed. It includes functionalities for detecting triangles, squares, rectangles, and circles, as well as identifying specific colors like blue and green. Additionally, it describes face detection and the application of filters such as blurring and overlaying images like sunglasses and hats on detected faces.

Uploaded by

Sara Labiad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

A4_exam

The document outlines a computer vision application using OpenCV to detect shapes, colors, and faces in real-time from a webcam feed. It includes functionalities for detecting triangles, squares, rectangles, and circles, as well as identifying specific colors like blue and green. Additionally, it describes face detection and the application of filters such as blurring and overlaying images like sunglasses and hats on detected faces.

Uploaded by

Sara Labiad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Detect Shapes: key = cv.

waitKey(1)

import cv2 as cv if key == ord('b'):

cam = cv.VideoCapture(0) HSV = [hsv_min, hsv_max]

text_form = '' print("Blue color:")

while not cv.waitKey(1) == 27: print(HSV)

_, img = cam.read() np.save('BlueColor', HSV)

img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) if key == ord('g'):

img_blur = cv.GaussianBlur(img_gray, (3, 3), 2) HSV = [hsv_min, hsv_max]

img_canny = cv.Canny(img_blur, 10, 100) print("Green color:")

contour, _ = cv.findContours(img_canny, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_NONE) print(HSV)

for cnt in contour: np.save('GreenColor', HSV)

cv.drawContours(img, contour, -1, (255, 0, 0), 1) cv.imshow("Mask", mask)

area = cv.contourArea(cnt) cv.imshow("image", img_result)

peri = cv.arcLength(cnt, True) Use detected Color:

approx = cv.approxPolyDP(cnt, 0.02*peri, True) import cv2 as cv

x, y, w, h = cv.boundingRect(approx) import numpy as np

cv.rectangle(img, (x, y), (x + w, y + h), (0, 0, 0), 3) blue = np.load('BlueColor.npy')

nb_point = len(approx) green = np.load('GreenColor.npy')

if nb_point == 3: def vide(a: int) -> None:

text_form = 'Triangle' pass

elif nb_point == 4: cam = cv.VideoCapture(0)

d = w / h while not cv.waitKey(1) == 27:

if 0.95 < d < 1.05: _, img = cam.read()

text_form = 'Square' img_hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)

else: mask_green = cv.inRange(img_hsv, green[0], green[1])

text_form = 'Rectangle' img_green = cv.bitwise_and(img, img, mask=mask_green)

else: mask_blue = cv.inRange(img_hsv, blue[0], blue[1])

text_form = 'Circle' img_blue = cv.bitwise_and(img, img, mask=mask_blue)

cv.putText(img, text_form, (x + 10, y + 20), <FONT>, 1, (0, 255, 0)) cv.imshow("blue", img_blue)

cv.imshow("Contours", img) cv.imshow("green", img_green)

Detect color and draw contour:

Detect Color: import cv2 as cv

import cv2 as cv import numpy as np

import numpy as np def detect(image, color, color_name, arr_contours):

def vide(a: int) -> None: for cnt in arr_contours:

pass _area = cv.contourArea(cnt)

cam = cv.VideoCapture(0) if _area > 300:

cv.namedWindow("TrackBar") _x, _y, _w, _h = cv.boundingRect(cnt)

cv.resizeWindow("TrackBar", 450, 230) cv.rectangle(image, (_x, _y), (_x + _w, _y + _h), color, 2)

cv.createTrackbar("h min", "TrackBar", 0, 255, vide) cv.putText(image, color_name, (_x, _y), cv.FONT_HERSHEY_SIMPLEX,

cv.createTrackbar("h max", "TrackBar", 255, 255, vide) 1.0,color)

cv.createTrackbar("s min", "TrackBar", 0, 255, vide) green = np.array([[25, 52, 72], [102, 255, 255]])

cv.createTrackbar("s max", "TrackBar", 255, 255, vide) blue = np.array([[94, 80, 2], [120, 255, 255]])

cv.createTrackbar("v min", "TrackBar", 0, 255, vide) cam = cv.VideoCapture(0)

cv.createTrackbar("v max", "TrackBar", 255, 255, vide) while not cv.waitKey(1) == 27:

while not cv.waitKey(1) == 27: _, img = cam.read()

_, img = cam.read() img_hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)

img_hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV) mask_green = cv.inRange(img_hsv, green[0], green[1])

h_min = cv.getTrackbarPos("h min", "TrackBar") img_green = cv.bitwise_and(img, img, mask=mask_green)

h_max = cv.getTrackbarPos("h max", "TrackBar") mask_blue = cv.inRange(img_hsv, blue[0], blue[1])

s_min = cv.getTrackbarPos("s min", "TrackBar") img_blue = cv.bitwise_and(img, img, mask=mask_blue)

s_max = cv.getTrackbarPos("s max", "TrackBar") green_countours, unused = cv.findContours(mask_green, cv.RETR_EXTERNAL,

v_min = cv.getTrackbarPos("v min", "TrackBar") cv.CHAIN_APPROX_NONE)

v_max = cv.getTrackbarPos("v max", "TrackBar") blue_contours, unused1 = cv.findContours(mask_blue, cv.RETR_EXTERNAL,

hsv_min = np.array([h_min, s_min, v_min]) cv.CHAIN_APPROX_NONE)

hsv_max = np.array([h_max, s_max, v_max]) detect(img, (0, 255, 0), "Green", green_countours)

mask = cv.inRange(img_hsv, hsv_min, hsv_max) detect(img, (255, 0, 0), "Blue", blue_contours)

img_result = cv.bitwise_and(img, img, mask=mask) cv.imshow("Image", img)


Detect face, profile and eyes: visage = visage_cascade.detectMultiScale(img_gray, 1.3, 4)

import cv2 as cv for x, y, w, h in visage:

cap = cv.VideoCapture(0) roi = img[y-marge:y+h+marge, x:x+w]

visage_cascade = cv.CascadeClassifier('../SEANCE_8/visage.xml') roi_blur = cv.GaussianBlur(roi, (kernel, kernel), 15)

profile_cascade = cv.CascadeClassifier('profile.xml') img[y-marge:y+h+marge, x:x+w] = roi_blur

yeux_cascade = cv.CascadeClassifier('yeux.xml') cv.imshow('Image', img)

while True: key = cv.waitKey(1)

_, img = cap.read() if key == ord('p') and kernel < 26:

img = cv.flip(img, 1) kernel += 2

img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) if key == ord('m') and kernel > 2:

visage = visage_cascade.detectMultiScale(img_gray, 1.3, 4) kernel -= 2

print(len(visage)) if cv.waitKey(1) == 27:

profile = profile_cascade.detectMultiScale(img_gray, 1.3, 4) break

V = len(visage) Mustache, sunglasses, hat:

P = len(profile) import cv2 as cv

print(V + P) visage_cascade = cv.CascadeClassifier('../SEANCE_8/visage.xml')

for x, y, w, h in visage: lunettes_img = cv.imread('lunettes/5.png', -1)

cv.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2) chapeau_img = cv.imread('chapeau.png', -1)

ROI_color = img[y:y + h, x:x + w] moustache_img = cv.imread('moustache.png', -1)

ROI = img_gray[y:y + h, x:x + w] cap = cv.VideoCapture(0)

yeux = yeux_cascade.detectMultiScale(ROI, 1.3, 4) def objet(img_pos, hauteur, largeur, image):

for x_, y_, w_, h_ in yeux: for i in range(hauteur):

cv.circle(ROI_color, (x_ + w_ // 2, y_ + h // 2), 20, (0, 0, 255), 2) for j in range(largeur):

for x, y, w, h in profile: if image[i, j, 3] != 0:

cv.rectangle(img, (x, y), (x + w, y + h), (0, 255, 255), 2) img_pos[i, j] = image[i, j, :3]

cv.imshow('Visage', img) while not cv.waitKey(1) == 27:

if cv.waitKey(1) == 27: _, img = cap.read()

break img = cv.flip(img, 1)

Screen face: img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

import cv2 as cv visage = visage_cascade.detectMultiScale(img_gray, 1.3, 4, minSize=(80, 80))

import time for x, y, w, h in visage:

cap = cv.VideoCapture(0) if w * h > 80000:

visage_cascade = cv.CascadeClassifier('visage.xml') l_min = int(y + 0.3 * h)

i = 1 l_max = int(y + 0.5 * h)

while i <= 10: l_hauteur = l_max - l_min

_, img = cap.read() m_min = int(y + 0.7 * h)

img = cv.flip(img, 1) m_max = int(y + 0.9 * h)

img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) m_hauteur = m_max - m_min

visage = visage_cascade.detectMultiScale(img_gray, 1.3, 4) m_x_min = int(x + 0.2 * w)

for x, y, w, h in visage: m_x_max = int(x + 0.8 * w)

face = img[y:y+h, x:x+w] m_largeur = m_x_max - m_x_min

cv.imwrite(str(i) + ".jpg", face) c_min = int(y - 0.95 * h)

cv.imshow(f"cap {i}", face) c_max = y + 20

cv.waitKey(0) c_hauteur = c_max - c_min

cv.destroyWindow(f"cap {i}") l_pos = img[l_min:l_max, x:x + w]

i += 1 m_pos = img[m_min:m_max, m_x_min:m_x_max]

time.sleep(1) c_pos = img[c_min:c_max, x:x + w]

cap.release() lunettes = cv.resize(lunettes_img, (w, l_hauteur))

Blur face: moustache = cv.resize(moustache_img, (m_largeur, m_hauteur))

import cv2 as cv chapeau = cv.resize(chapeau_img, (w, c_hauteur))

import numpy as np objet(l_pos, l_hauteur, w, lunettes)

cap = cv.VideoCapture(0) objet(m_pos, m_hauteur, w, chapeau)

visage_cascade = cv.CascadeClassifier('../SEANCE_8/visage.xml') objet(c_pos, c_hauteur, w, moustache)

marge = 30 cv.imshow('filtre', img)

kernel = 3

while True:

_, img = cap.read()

img = cv.flip(img, 1)

img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy