以下は秀和システムの『Python 実践データ分析 100本ノック』のP220、ノック86のコードです。
Python3
1import cv2 2import dlib 3import math 4 5# 準備 # 6predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") 7detector = dlib.get_frontal_face_detector() 8 9# 検出 # 10img = cv2.imread("img/img02.jpg") 11dets = detector(img, 1) 12 13for k, d in enumerate(dets): 14 shape = predictor(img, d) 15 16 # 顔領域の表示 17 color_f = (0, 0, 225) 18 color_l_out = (255, 0, 0) 19 color_l_in = (0, 255, 0) 20 line_w = 3 21 circle_r = 3 22 fontType = cv2.FONT_HERSHEY_SIMPLEX 23 fontSize = 1 24 cv2.rectangle(img, (d.left(), d.top()), (d.right(), d.bottom()), color_f, line_w) 25 cv2.putText(img, str(k), (d.left(), d.top()), fontType, fontSize, color_f, line_w) 26 27 # 重心を導出する箱を用意 28 num_of_points_out = 17 29 num_of_points_in = shape.num_parts - num_of_points_out 30 gx_out = 0 31 gy_out = 0 32 gx_in = 0 33 gy_in = 0 34 for shape_point_count in range(shape.num_parts): 35 shape_point = shape.part(shape_point_count) 36 #print("顔器官No.{} 座標位置: ({},{})".format(shape_point_count, shape_point.x, shape_point.y)) 37 #器官ごとに描画 38 if shape_point_count<num_of_points_out: 39 cv2.circle(img,(shape_point.x, shape_point.y),circle_r,color_l_out, line_w) 40 gx_out = gx_out + shape_point.x/num_of_points_out 41 gy_out = gy_out + shape_point.y/num_of_points_out 42 else: 43 cv2.circle(img,(shape_point.x, shape_point.y),circle_r,color_l_in, line_w) 44 gx_in = gx_in + shape_point.x/num_of_points_in 45 gy_in = gy_in + shape_point.y/num_of_points_in 46 47 # 重心位置を描画 48 cv2.circle(img,(int(gx_out), int(gy_out)),circle_r,(0,0,255), line_w) 49 cv2.circle(img,(int(gx_in), int(gy_in)),circle_r,(0,0,0), line_w) 50 51 # 顔の方位を計算 52 theta = math.asin(2*(gx_in-gx_out)/(d.right()-d.left())) 53 radian = theta*180/math.pi 54 print("顔方位:{} (角度:{}度)".format(theta,radian)) 55 56 # 顔方位を表示 57 if radian<0: 58 textPrefix = " left " 59 else: 60 textPrefix = " right " 61 textShow = textPrefix + str(round(abs(radian),1)) + " deg." 62 cv2.putText(img, textShow, (d.left(), d.top()), fontType, fontSize, color_f, line_w) 63 64 65cv2.imshow("img",img) 66cv2.imwrite("temp.jpg",img) 67cv2.waitKey(0)
ここで分からない部分があります。
dets = detector(img, 1)
の第2引数の1はいったい何を意味しているのでしょうか?
マニュアルを読んだり検索したのですが、よく分かりませんでした。
お答えいただけたらありがたいです。
宜しくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/04/08 12:18