1. ã¯ããã«
ãã®è¨äºã§ã¯ãç§ãCopilotã¨å¯¾è©±ãéãããã®çµé¨ãå ±æãã¾ããç§ã®ç®æ¨ã¯ãPythonã使ã£ã¦ç»åã«ã¢ã¶ã¤ã¯å¦çãæ½ãããã°ã©ã ãä½æãããã¨ã§ãããã®ã¢ã¤ãã¢ã¯ãProcessingã¨ããã¢ã¼ãã£ã¹ãã»ã¯ãªã¨ã¤ã¿ã¼åãã®ããã°ã©ã è¨èªã使ã£ããã¡ãã®è¨äºããå¾ã¾ããã
2. Copilotã¨ã®å¯¾è©±
ç§ãCopilotã«æ±ããã®ã¯ããPythonã§ã¤ã¡ã¼ã¸ãã¡ã¤ã«ãèªã¿è¾¼ã¿ã人ã®é¡ãèªèãã¦ã¢ã¶ã¤ã¯ãæããã³ã¼ããã®çæã§ãããã¾ããå¿ è¦ãªã©ã¤ãã©ãªãããã°ãããã«ã¤ãã¦ãæãã¦ãããã¾ããã
2.1 ç§ã®è³ªå
Pythonã§ã¤ã¡ã¼ã¸ãã¡ã¤ã«ãèªã¿è¾¼ã¿ã人ã®é¡ãèªèãã¦ã¢ã¶ã¤ã¯ãæããã³ã¼ããæãã¦ãã ãããã¤ã³ã¹ãã¼ã«ãå¿ è¦ãªã©ã¤ãã©ãªãããå ´åã¯ãããã«ã¤ãã¦ãæãã¦ãã ããã
2.2 Copilotã®åç
Pythonã§ç»åãèªã¿è¾¼ã¿ãé¡ãèªèãããã®é¡ã«ã¢ã¶ã¤ã¯ããããããã«ã¯ã
opencv-python
ã¨numpy
ã¨ããã©ã¤ãã©ãªãå¿ è¦ã§ãããããã®ã©ã¤ãã©ãªã¯ä»¥ä¸ã®ã³ãã³ãã§ã¤ã³ã¹ãã¼ã«ã§ãã¾ããpip install opencv-python numpy
以ä¸ã«ãæå®ããç»åããé¡ãæ¤åºãããã®é¡ã«ã¢ã¶ã¤ã¯ãããããµã³ãã«ã³ã¼ãã示ãã¾ãã
import cv2 import numpy as np def mosaic(img, ratio=0.1): small = cv2.resize(img, None, fx=ratio, fy=ratio, interpolation=cv2.INTER_NEAREST) return cv2.resize(small, img.shape[:2][::-1], interpolation=cv2.INTER_NEAREST) # é¡æ¤åºç¨ã®ã«ã¹ã±ã¼ããã¡ã¤ã«ãæå® cascade_path = "/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml" cascade = cv2.CascadeClassifier(cascade_path) # ç»åãèªã¿è¾¼ã img = cv2.imread("image.jpg") # ã°ã¬ã¼ã¹ã±ã¼ã«ã«å¤æ gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # é¡ãæ¤åº faces = cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=3, minSize=(30, 30)) # é¡é¨åã«ã¢ã¶ã¤ã¯ãããã for (x, y, w, h) in faces: face_img = img[y:y+h, x:x+w] img[y:y+h, x:x+w] = mosaic(face_img) # ç»åãä¿å cv2.imwrite("mosaic.jpg", img)
ãã®ã³ã¼ãã¯ãæå®ããç»å(
image.jpg
)ããé¡ãæ¤åºãããã®é¡ã«ã¢ã¶ã¤ã¯ããããçµæãmosaic.jpg
ã¨ãã¦ä¿åãã¾ããã¢ã¶ã¤ã¯ã®ç²ãã¯mosaic
é¢æ°ã®ratio
ãã©ã¡ã¼ã¿ã§èª¿æ´ã§ãã¾ããå¤ãå°ããã»ã©ãã¢ã¶ã¤ã¯ã¯ç²ããªãã¾ãããªãããã®ã³ã¼ãã¯OpenCVã®ããã©ã«ãã®ã«ã¹ã±ã¼ããã¡ã¤ã«ã使ç¨ãã¦ãã¾ããç°ãªãç°å¢ã§ã¯ãã«ã¹ã±ã¼ããã¡ã¤ã«ã®ãã¹ãç°ãªãå¯è½æ§ããããããé©åãªãã¹ã«å¤æ´ãã¦ãã ãããã¾ãããã®ã³ã¼ãã¯æ£é¢ããè¦ãé¡ã®ã¿ãæ¤åºãã¾ãããããã£ã¼ã«ãªã©ãä»ã®è§åº¦ããè¦ãé¡ãæ¤åºããã«ã¯ãå¥ã®ã«ã¹ã±ã¼ããã¡ã¤ã«ã使ç¨ããå¿ è¦ãããã¾ããå ·ä½çãªãã¹ãã«ã¹ã±ã¼ããã¡ã¤ã«ã¯ã使ç¨ãã¦ããç°å¢ãè¦ä»¶ã«ããã¾ããå¿ è¦ã«å¿ãã¦é©åãªãã®ãé¸æãã¦ãã ããã
ãã®ã³ã¼ãã¯åºæ¬çãªãã®ã§ãå®éã®ä½¿ç¨ã«ã¯ãããªã調æ´ãå¿ è¦ããããã¾ãããä¾ãã°ãé¡æ¤åºã®ç²¾åº¦ãä¸ããããã«ã¯ã
detectMultiScale
ã¡ã½ããã®ãã©ã¡ã¼ã¿ã調æ´ãããã¨ãèãããã¾ããã¾ããç»åã®æãããã³ã³ãã©ã¹ããé¡ã®å¤§ãããä½ç½®ãªã©ãç»åã®ç¹æ§ã«ãã£ã¦ã¯ãé¡æ¤åºããã¾ããããªãå ´åãããã¾ãããã®ãããªå ´åã«ã¯ãç»åã®åå¦çããã©ã¡ã¼ã¿ã®èª¿æ´ãå¿ è¦ã«ãªãããããã¾ãããå ·ä½çãªèª¿æ´æ¹æ³ã¯ã使ç¨ããç»åãè¦ä»¶ã«ããã¾ããå¿ è¦ã«å¿ãã¦é©åãªèª¿æ´ãè¡ã£ã¦ãã ããã以ä¸ãåºæ¬çãªé¡æ¤åºã¨ã¢ã¶ã¤ã¯å¦çã®ã³ã¼ãã§ããä½ãä»ã«è³ªåãããã¾ãããããæ°è»½ã«ã©ããã
ã½ã¼ã¹: Copilot ã¨ã®ä¼è©±ã 2024/6/13 (1) github.com. https://github.com/vhehduatks/AI_mosaic/tree/2f181e682f2fff53d177f01f12aaf4ce065b6df1/detector.py.
ã©ã¤ãã©ãªã®ã¤ã³ã¹ãã¼ã«
ç§ã®Windows PCã«ã¯ãPythonãã¤ã³ã¹ãã¼ã«æ¸ã¿ã§ããCopilotã®æ¡å ã«å¾ããPythonã©ã¤ãã©ãªãã¤ã³ã¹ãã¼ã«ãã¦ããã¾ãã
pip install opencv-python numpy
ã«ã¹ã±ã¼ããã¡ã¤ã«ã®ãã¹å¤æ´
ç§ã®è³ªå
PowerShellã§ãhaarcascade_frontalface_alt.xmlãã¡ã¤ã«ãæ¤ç´¢ããã³ãã³ããæãã¦ãã ããã
Copilotã®åç
PowerShellã§ç¹å®ã®ãã¡ã¤ã«ãæ¤ç´¢ããã«ã¯ã
Get-ChildItem
ã³ãã³ãã¬ããã使ç¨ãã¾ãã以ä¸ã«ãhaarcascade_frontalface_alt.xml
ãã¡ã¤ã«ãæ¤ç´¢ããããã®ã³ãã³ãã示ãã¾ããGet-ChildItem -Path C:\ -Filter haarcascade_frontalface_alt.xml -Recurse -ErrorAction SilentlyContinue -Force
ãã®ã³ãã³ãã¯ãCãã©ã¤ãå ¨ä½ã対象ã«
haarcascade_frontalface_alt.xml
ã¨ããååã®ãã¡ã¤ã«ãæ¤ç´¢ãã¾ãã-Recurse
ãªãã·ã§ã³ã«ããããã¹ã¦ã®ãµããã£ã¬ã¯ããªãæ¤ç´¢å¯¾è±¡ã¨ãªãã¾ãã-ErrorAction SilentlyContinue
ã¯ãã¢ã¯ã»ã¹æ¨©éããªããã£ã¬ã¯ããªãªã©ã§ã¨ã©ã¼ãçºçããå ´åã«ãã¨ã©ã¼ã¡ãã»ã¼ã¸ã表示ããã«å¦çãç¶è¡ããããã®ãªãã·ã§ã³ã§ãã-Force
ãªãã·ã§ã³ã¯ãé ããã¡ã¤ã«ãã·ã¹ãã ãã¡ã¤ã«ãæ¤ç´¢å¯¾è±¡ã«å«ããããã®ãªãã·ã§ã³ã§ãããªãããã®ã³ãã³ãã¯Cãã©ã¤ãå ¨ä½ãæ¤ç´¢ãããããå®è¡ã«ã¯æéããããå ´åãããã¾ããã¾ããã·ã¹ãã ãã¡ã¤ã«ãå«ãã¦æ¤ç´¢ããããã管çè 権éãå¿ è¦ãªå ´åãããã¾ããå¿ è¦ã«å¿ãã¦ãæ¤ç´¢å¯¾è±¡ã®ãã¹ããªãã·ã§ã³ã調æ´ãã¦ãã ããã
以ä¸ãåºæ¬çãªãã¡ã¤ã«æ¤ç´¢ã®ã³ãã³ãã§ããä½ãä»ã«è³ªåãããã¾ãããããæ°è»½ã«ã©ããã
ã«ã¹ã±ã¼ããã¡ã¤ã«ã®ãã¹ç¢ºèª
PowerShellã§haarcascade_frontalface_alt.xml
ãã¡ã¤ã«ã®ãã¹ã確èªãã¾ãã
PS C:\Users\user1> Get-ChildItem -Path C:\ -Filter haarcascade_frontalface_alt.xml -Recurse -ErrorAction SilentlyContinue -Force Directory: C:\Users\user1\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\cv2\data Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 2024/06/13 11:30 676709 haarcascade_frontalface_alt.xml
haarcascade_frontalface_alt.xml
ãã¡ã¤ã«ã®ãã¹ã¯ã以ä¸ã®éãã§ãããã¨ãåããã¾ããã
C:\Users\user1\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\cv2\data\haarcascade_frontalface_alt.xml
é¡æ¤åºç¨ã®ã«ã¹ã±ã¼ããã¡ã¤ã«ãã¹ã®ä¿®æ£
ã½ã¼ã¹ã³ã¼ãã以ä¸ã®ããã«ä¿®æ£ãã¾ããã
# é¡æ¤åºç¨ã®ã«ã¹ã±ã¼ããã¡ã¤ã«ãæå® cascade_path = "C:\Users\user1\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\cv2\data\haarcascade_frontalface_alt.xml"
ã½ã¼ã¹ã³ã¼ãã®ä¿å
Pythonã®ã½ã¼ã¹ã³ã¼ããauto-face-detect-mosaic.py
ã¨ãããã¡ã¤ã«åã§ãä»»æã®ãã©ã«ãã«ä¿åãã¾ãã
ã¢ã¶ã¤ã¯å¦ç対象ã®ç»å
å¦ç対象ã®ç»åã¯ãçæAIã§ä½æããå®å¨ããªã人ã®é¡ç»åã§ããimage.jpgã¨ãããã¡ã¤ã«åã«ãªãã¼ã ããã½ã¼ã¹ã³ã¼ãã¨åããã©ã«ãã«ä¿åãã¦ããã¾ãã
ã¢ã¶ã¤ã¯å¦çã®å®è¡ï¼ï¼SyntaxError
Pythonã³ã¼ããå®è¡ããã¨ãããã¨ã©ã¼ã¨ãªãã¾ãããèªåã§æ¸ãæãããcascade_path
ã®ã¨ãããã¨ã©ã¼ã«ãªã£ã¦ãã¾ããããããã\ï¼ããã¯ã¹ã©ãã·ã¥ï¼ããåé¡ãªã®ã ã¨æãã¾ããããããCopilotã«èããã¨ã«ãã¾ãã
PS C:\auto-face-detect-mosaic> python .\auto-face-detect-mosaic.py File ".\auto-face-detect-mosaic.py", line 9 cascade_path = "C:\Users\user1\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\cv2\data\haarcascade_frontalface_alt.xml" ^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
ã¨ã©ã¼ã®å¯¾å¦æ¹æ³ãCopilotã«èã
ç§ã®è³ªå
ãã®ãããªã¨ã©ã¼ã表示ããã¾ããã対å¦æ¹æ³ãæãã¦ãã ããã
PS C:\auto-face-detect-mosaic> python .\auto-face-detect-mosaic.py File ".\auto-face-detect-mosaic.py", line 9 cascade_path = "C:\Users\user1\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\cv2\data\haarcascade_frontalface_alt.xml" ^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
Copilotã®åç
ã¨ã©ã¼ã¡ãã»ã¼ã¸ã«ããã¨ãPythonã®æååå ã§ããã¯ã¹ã©ãã·ã¥(
\
)ãã¨ã¹ã±ã¼ãã·ã¼ã±ã³ã¹ã¨ãã¦è§£éããã¦ãã¾ããUnicodeã¨ã¹ã±ã¼ãã·ã¼ã±ã³ã¹(\UXXXXXXXX
)ã¨ãã¦è§£éããã¦ãã¾ã£ãããã«ã¨ã©ã¼ãçºçãã¦ãã¾ããPythonã§ã¯ãããã¯ã¹ã©ãã·ã¥(
\
)ã¯ã¨ã¹ã±ã¼ãã·ã¼ã±ã³ã¹ã®éå§ã示ãç¹æ®ãªæåã§ãããããã£ã¦ãWindowsã®ãã¹ã®ããã«ããã¯ã¹ã©ãã·ã¥ãå«ãæååããã®ã¾ã¾Pythonã®æååã¨ãã¦ä½¿ç¨ããã¨ããã®ãããªã¨ã©ã¼ãçºçãã¾ãããã®åé¡ã解決ããã«ã¯ã以ä¸ã®ããããã®æ¹æ³ã試ãã¦ã¿ã¦ãã ããã
- æååãrawæååï¼rãå é ã«ã¤ããæååï¼ã¨ãã¦æ±ããããã«ãããããã¯ã¹ã©ãã·ã¥ãã¨ã¹ã±ã¼ãã·ã¼ã±ã³ã¹ã¨ãã¦è§£éãããã®ãé²ããã¨ãã§ãã¾ãã
cascade_path = r"C:\Users\user1\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\cv2\data\haarcascade_frontalface_alt.xml"
- ããã¯ã¹ã©ãã·ã¥ãäºéã«ãããããã«ãããä¸ã¤ç®ã®ããã¯ã¹ã©ãã·ã¥ãäºã¤ç®ã®ããã¯ã¹ã©ãã·ã¥ãã¨ã¹ã±ã¼ãããçµæã¨ãã¦ããã¯ã¹ã©ãã·ã¥ä¸ã¤ãå¾ããã¾ãã
cascade_path = "C:\\Users\\user1\\AppData\\Local\\Programs\\Python\\Python38-32\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalface_alt.xml"
ãããã®ä¿®æ£ãè¡ããã¨ã§ãã¨ã©ã¼ã解æ¶ããã¯ãã§ããããã§ãåé¡ã解決ããªãå ´åã¯ãå度ãç¥ãããã ããããããããé¡ããããã¾ãã
é¡æ¤åºç¨ã®ã«ã¹ã±ã¼ããã¡ã¤ã«ãã¹ã®ä¿®æ£ï¼\ãã¨ã¹ã±ã¼ãï¼
æ®æ®µPythonã使ã£ã¦ããªããããç°¡åãªãã¨ã§ãã¤ã¾ããã¾ããã2. ããã¯ã¹ã©ãã·ã¥ãäºéã«ããããã§å¯¾å¦ãã¾ããã½ã¼ã¹ã³ã¼ãã以ä¸ã®ããã«ä¿®æ£ãã¾ããã
# é¡æ¤åºç¨ã®ã«ã¹ã±ã¼ããã¡ã¤ã«ãæå® cascade_path = "C:\\Users\\user1\\AppData\\Local\\Programs\\Python\\Python38-32\\Lib\site-packages\\cv2\\data\\haarcascade_frontalface_alt.xml"
ã½ã¼ã¹ã³ã¼ãå ¨æ
auto-face-detect-mosaic.py
import cv2 import numpy as np def mosaic(img, ratio=0.1): small = cv2.resize(img, None, fx=ratio, fy=ratio, interpolation=cv2.INTER_NEAREST) return cv2.resize(small, img.shape[:2][::-1], interpolation=cv2.INTER_NEAREST) # é¡æ¤åºç¨ã®ã«ã¹ã±ã¼ããã¡ã¤ã«ãæå® cascade_path = "C:\\Users\\user1\\AppData\\Local\\Programs\\Python\\Python38-32\\Lib\site-packages\\cv2\\data\\haarcascade_frontalface_alt.xml" cascade = cv2.CascadeClassifier(cascade_path) # ç»åãèªã¿è¾¼ã img = cv2.imread("image.jpg") # ã°ã¬ã¼ã¹ã±ã¼ã«ã«å¤æ gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # é¡ãæ¤åº faces = cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=3, minSize=(30, 30)) # é¡é¨åã«ã¢ã¶ã¤ã¯ãããã for (x, y, w, h) in faces: face_img = img[y:y+h, x:x+w] img[y:y+h, x:x+w] = mosaic(face_img) # ç»åãä¿å cv2.imwrite("mosaic.jpg", img)
ã¢ã¶ã¤ã¯å¦çã®å®è¡ï¼ï¼æå
å®è¡
Pythonã³ã¼ããå度å®è¡ããã¨ãããå®è¡ã«æåãã¾ããã
PS C:\auto-face-detect-mosaic> python .\auto-face-detect-mosaic.py PS C:\auto-face-detect-mosaic>
çµæç»å
é¡ã®é¨åã«ã¢ã¶ã¤ã¯å¦çãæ½ããããã¨ã確èªãã¾ãã
ã¢ã¶ã¤ã¯å¦çã®å®è¡ï¼ï¼ã¢ã¶ã¤ã¯ãç²ããã
ã¢ã¶ã¤ã¯ã®ç²ãã¯
mosaic
é¢æ°ã®ratio
ãã©ã¡ã¼ã¿ã§èª¿æ´ã§ãã¾ããå¤ãå°ããã»ã©ãã¢ã¶ã¤ã¯ã¯ç²ããªãã¾ãã
ããå°ãã¢ã¶ã¤ã¯ãç²ããã¦ã¿ãããªãã¾ãããmosaic
é¢æ°ã®ratio
ãã©ã¡ã¼ã¿ãç¾å¨ã®0.1
ããã0.025
ã«å¤ãã¦ã¿ã¾ãã
ãã£ã¦ã¿ãææ³
- ç§ã®ãããªPythonã®ç´ 人ã§ããã©ã¤ãã©ãªã®å°å ¥ãç»åã®é¡èªèãã¢ã¶ã¤ã¯é©ç¨ã¨ããå¦çãå®è£ ã§ãã¦ãã¾ãã¾ããã
- ã»ã¨ãã©èªåã§èãããã¨ãªããã³ã¼ããå®è£ ã§ãã¾ããã
- ã¨ã©ã¼ã«ã¤ãã¦ããCopilotã«èãã¦å¯¾å¦ã§ãã¾ããã
- ä»åä½æããã³ã¼ããå ã«ã人ã®é¡ä»¥å¤ãèªèã§ããã³ã¼ããä½æãã¦ã¿ãããªãã¾ããã
- æ¬å½ã¯Copilotã«ããã®ç»åã®äººã®é¡ã«ã¢ã¶ã¤ã¯ãæãã¦ãã ãããã¨é ¼ã¿ããã¨ããã§ãããããã®å¦çãè¡ãã½ã¼ã¹ã³ã¼ããä½ã£ã¦ããããã¨ããã®ãããæ¹æ³ã ã¨æãã¾ããã