|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# ==================================================================== # DISCLAIMER: # This code is provided as-is for educational and experimental # purposes only. The author makes no representations or warranties of # any kind concerning the safety, suitability, or accuracy of this # code. Use at your own risk. The author assumes no liability for any # damages, system failures, security breaches, or network issues # resulting from the use or implementation of this script. # ==================================================================== import cv2 import time from picamera2 import Picamera2 import mediapipe as mp import numpy as np ################################# import board import adafruit_ssd1306 from PIL import Image, ImageDraw, ImageFont import time wOLED = 128 hOLED = 64 I2C_ADDRESS = 0x3C i2c = board.I2C() oled = adafruit_ssd1306.SSD1306_I2C(wOLED, hOLED, i2c, addr = I2C_ADDRESS) ################################# W=1280 H=720 tStart = time.time() fps = 0 piCam = Picamera2(1) piCam.preview_configuration.main.size = (W, H) piCam.preview_configuration.main.format = "RGB888" piCam.preview_configuration.controls.FrameRate = 60 piCam.preview_configuration.align() piCam.configure("preview") piCam.start() textLowerLeft = (int(W*.01),int(H*.05)) fontFace = cv2.FONT_HERSHEY_SIMPLEX fontThickness = int(W/425) fontScale = H*.0015 fontColor = (0,0,255) faceMesh = mp.solutions.face_mesh.FaceMesh( max_num_faces=1, refine_landmarks=True, min_detection_confidence=0.5, min_tracking_confidence=0.5) cv2.namedWindow('Camera',cv2.WINDOW_GUI_NORMAL) cv2.moveWindow('Camera',0,65) cv2.resizeWindow('Camera',W,H) while True: deltaT = time.time() - tStart tStart=time.time() fps = fps*.95 + (1/deltaT)*.05 frame = piCam.capture_array() frame = cv2.flip(frame, -1) rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) results = faceMesh.process(rgb) if results.multi_face_landmarks: for faceLandMarks in results.multi_face_landmarks: #print(faceLandmarks) image = Image.new("1",(wOLED,hOLED)) draw = ImageDraw.Draw(image) lmAll = [] for lm in faceLandMarks.landmark: xOLED = int(lm.x*wOLED) yOLED = int(lm.y*hOLED) draw.point((xOLED,yOLED), fill = 255) #print(xOLED,yOLED) lmAll.append((xOLED,yOLED)) for idx in mp.solutions.face_mesh.FACEMESH_CONTOURS: #print(idx) lineStart = idx[0] lineEnd = idx[1] pt1 = lmAll[lineStart] pt2 = lmAll[lineEnd] draw.line((pt1[0],pt1[1],pt2[0],pt2[1]), width=1, fill = 255) #print('XXXXXXXXXXXXXXXXXXx') oled.image(image) oled.show() #print() # if results.multi_face_landmarks: # for faceLandmarks in results.multi_face_landmarks: # # Optional second pass for glow effect # mp.solutions.drawing_utils.draw_landmarks( # image=frame, # landmark_list=faceLandmarks, # connections=mp.solutions.face_mesh.FACEMESH_TESSELATION, # landmark_drawing_spec=None, # connection_drawing_spec=mp.solutions.drawing_utils.DrawingSpec( # color=(100, 100, 100), # thickness=1 # ) # ) # # === Best looking translucent mesh === # mp.solutions.drawing_utils.draw_landmarks( # image=frame, # landmark_list=faceLandmarks, # connections=mp.solutions.face_mesh.FACEMESH_CONTOURS, # landmark_drawing_spec=None, # Hide dots # connection_drawing_spec=mp.solutions.drawing_utils.DrawingSpec( # color=(0, 0, 180), # Bright cyan/teal # thickness=7 # ) # ) # # # Optional: Extra detail on irises (makes eyes look better) # mp.solutions.drawing_utils.draw_landmarks( # image=frame, # landmark_list=faceLandmarks, # connections=mp.solutions.face_mesh.FACEMESH_IRISES, # landmark_drawing_spec=None, # connection_drawing_spec=mp.solutions.drawing_utils.DrawingSpec( # color=(255, 0, 0), # thickness=7 # ) # ) myText = "FPS: "+str(round(fps,1)) cv2.putText(frame,myText,textLowerLeft,fontFace,fontScale,fontColor,fontThickness) cv2.imshow("Camera", frame) if cv2.waitKey(1) == ord('q'): break cv2.destroyAllWindows() piCam.stop() oled.fill(0) oled.show() |
In this lesson we are still using our standard Fusion AI Lab Kit cirtuit from the earlier lessons:

We have also connected the OLED to the above circuit according to this diagram:

