[ubuntu20.04] openCVをinstallしたい

$ sudo apt-get update
$ sudo apt-get install libopencv-dev python3-opencv
$ python3
Python 3.8.10 (default, Sep 28 2021, 16:10:42)
[GCC 9.3.0] on linux
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import cv2
>>> cv2.__version__
‘4.2.0’

$ sudo apt-get install python3-pip
$ pip3 install pillow

import cv2

img = cv2.imread("src/test.jpg")

imgG = img.mean(axis=2)

from PIL import Image
imgP = Image.fromarray(imgG)
imgP = imgP.convert('RGB')
imgP.save('src/test2.jpg')

$ wget https://github.com/opencv/opencv/blob/master/data/haarcascades/haarcascade_frontalface_alt.xml

### face-detection
$ wget https://raw.githubusercontent.com/opencv/opencv/master/data/haarcascades/haarcascade_frontalface_default.xml

import cv2
 
cascade_file = "haarcascade_frontalface_default.xml"
cascade = cv2.CascadeClassifier(cascade_file)
 
img = cv2.imread("src/face.jpg")
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 
face_list = cascade.detectMultiScale(img_gray, minSize=(50,50))
 
if len(face_list) == 0:
    print("false")
    quit()
 
for (x,y,w,h) in face_list:
    print(" 顔の座標=", x, y, w, h)
    red = (0, 0, 255)
    cv2.rectangle(img, (x, y), (x+w, y+h), red, thickness=5)
 
cv2.imwrite("src/face-detect.png", img)

マスク画像

マスクでも行けるのは行けるのか