def takePicture_CSI(personGroupId, delay, size='small'): # delay in ms 3000ms = 3s # jpgimagepath = os.path.join(basepath, 'takepictures', personGroupId + "_" + time.strftime( # "%Y%m%d_%H%M%S", time.localtime()) + ".jpg") picturepath = ClassUtils.getTakePicturePath(personGroupId) if not os.path.exists(os.path.dirname(picturepath)): os.makedirs(os.path.dirname(picturepath)) try: # small for 辨識,加快速度。 if size == 'small': subprocess.call([ 'raspistill', '-hf', '-w', '800', '-h', '450', '-t', str(delay), '-o', picturepath ]) else: # for 訓練。訓練用圖片可以比較大 subprocess.call([ 'raspistill', '-hf', '-w', '1600', '-h', '900', '-t', str(delay), '-o', picturepath ]) except OSError: # ClassMessageBox.FaceAPIErrorGUI('def takePicture_CSI', 'CSI 攝影機無法啟動!', # 'OSError: raspistill 無法執行或不存在!!') print('def takePicture_CSI', 'CSI 攝影機無法啟動!', 'OSError: raspistill 無法執行或不存在!!') return None #os.system("raspistill -t " + str(delay) + " -o " + imagepath) return picturepath
def show_opencv(typee, mirror=False): ''' 顯示主畫面 ''' import cv2 import numpy as np config = ClassUtils.loadConfig() cam = cv2.VideoCapture(config['videoid']) cam.set(3, 1280) # 修改解析度 寬 cam.set(4, 1280 // 16 * 9) # 修改解析度 高 print('WIDTH', cam.get(3), 'HEIGHT', cam.get(4)) # 顯示預設的解析度 while True: ret_val, img = cam.read() # Add for face detection, ottowei , 2019011701 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Load the Haar cascade file face_cascade = cv2.CascadeClassifier( 'haar_cascade_files/haarcascade_frontalface_default.xml') # Run the face detector on the grayscale image face_rects = face_cascade.detectMultiScale(gray, 1.3, 5) # Draw a rectangle around the face for (x, y, w, h) in face_rects: cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 3) if mirror: img = cv2.flip(img, 1) H, W = img.shape[:2] #imS = cv2.resize(img, (W, H)) cv2_im = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # cv2和PIL中颜色的hex码的储存顺序不同 pil_im = Image.fromarray(cv2_im) draw = ImageDraw.Draw(pil_im) # 括号中为需要打印的canvas,这里就是在图片上直接打印 ttf = ClassUtils.getSystemFont() font = ImageFont.truetype(ttf, 40, encoding="utf-8") hintfont = ImageFont.truetype(ttf, 24, encoding="utf-8") title = config['title'] + "" w, h = draw.textsize(title, font=font) draw.rectangle(((W / 2 - w / 2 - 5, 0), (W / 2 + w / 2 + 5, h + 20)), fill="black") titlelocation = (W / 2 - w / 2, 5) #textlocation = (0,0) draw.text(titlelocation, title, (0, 255, 255), font=font) # 第一个参数为打印的坐标,第二个为打印的文本,第三个为字体颜色,第四个为字体 # FreeAPIKEY: b9160fbd882f47bd821205a4bce64354 if config['api_key'] == 'b9160fbd882f47bd821205a4bce64354': warningfont = ImageFont.truetype(ttf, 24, encoding="utf-8") warning = "請注意,您目前是用的是共用的測試 API_KEY 請儘速自行申請一個自用的 KEY" w, h = draw.textsize(warning, font=warningfont) draw.rectangle( ((W / 2 - w / 2 - 5, H - h * 2), (W / 2 + w / 2 + 5, H - h)), fill="yellow") warninglocation = (W / 2 - w / 2, H - h * 2) draw.text( warninglocation, warning, (0, 0, 255), font=warningfont) # 第一个参数为打印的坐标,第二个为打印的文本,第三个为字体颜色,第四个为字体 if typee == 'Identify': hint = "Please Press [ENTER] to Identify" elif typee == 'Train': hint = "Please Press [ENTER] to take triple photos" else: hint = "Please Press [ENTER] to continue" w, h = draw.textsize(hint, font=hintfont) draw.rectangle(((W / 2 - w / 2 - 5, H - h), (W / 2 + w / 2 + 5, H)), fill="red") hintlocation = (W / 2 - w / 2, H - h) #textlocation = (0,0) draw.text(hintlocation, hint, (0, 255, 255), font=hintfont) # 第一个参数为打印的坐标,第二个为打印的文本,第三个为字体颜色,第四个为字体 cv2_text_im = cv2.cvtColor(np.array(pil_im), cv2.COLOR_RGB2BGR) if ClassUtils.isWindows(): cv2.namedWindow("window", cv2.WND_PROP_FULLSCREEN) cv2.setWindowProperty("window", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) cv2.imshow("window", cv2_text_im) #cv2.imshow("window", img) key = cv2.waitKey(1) if key == ord(' ') or key == 3 or key == 13: # space or enter picturepath = ClassUtils.getTakePicturePath( config['personGroupId']) cv2.imwrite(picturepath, img) cv2.destroyAllWindows() cv2.VideoCapture(config['videoid']).release() return picturepath elif key == 27: # esc to quit cv2.destroyAllWindows() cv2.VideoCapture(config['videoid']).release() raise MyException.esc_opencv("偵測到 esc 結束鏡頭") else: if key != -1: print('key=', key)
def detectURLImages(self, imageurl): ''' 下載後,用 detectLocalImage 上傳辨識 ''' jpgimagepath = ClassUtils.getTakePicturePath(config['personGroupId']) print('jpgimagepath:', jpgimagepath) request.urlretrieve(imageurl, jpgimagepath) return self.detectLocalImage(jpgimagepath)