问题描述
这周我写了这段代码 challenge 来产生丑陋的数字。
sub factors( $n ) {
if $n > 1 {
$_,|factors $n div $_
given ( grep $n %% *,2..* ).first } }
.say for ( 1,|grep *.&factors.all ∈ ( 2,3,5 ),2..* )[^150];
这是有效的,从某种意义上说,它产生了正确的输出,但它的行为并不懒惰:输出不会立即开始,而是在开始 30 秒后开始。
但是,当我删除索引并遍历裸序列时
.say for 1,2..*;
我按预期立即得到输出。
这是一个错误,是吗?
解决方法
这不是错误(尽管可能记录得更好)。使用 import cv2
import face_recognition
import math
import csv
from os import path
# calculate Eye Aspect Ratio (EAR)
def cal_EAR(arr):
p2_p6 = math.sqrt(pow((arr[1][0] - arr[5][0]),2) + pow((arr[1][1] - arr[5][1]),2))
p3_p5 = math.sqrt(pow((arr[2][0] - arr[4][0]),2) + pow((arr[2][1] - arr[4][1]),2))
p1_p4 = math.sqrt(pow((arr[0][0] - arr[3][0]),2) + pow((arr[0][1] - arr[3][1]),2))
return (p2_p6 + p3_p5) / (2 * p1_p4)
# calculate Mouth Aspect Ratio (MAR)
def cal_MAR(arr_top,arr_bottom):
top_bottom = math.sqrt(pow(arr_top[-3][0] - arr_bottom[-3][0],2) + pow(arr_top[-3][1] - arr_bottom[-3][1],2))
left_right = math.sqrt(pow(arr_top[0][0] - arr_bottom[0][0],2) + pow(arr_top[0][1] - arr_bottom[0][1],2))
return top_bottom / left_right
# calculate pupil circularity (PUC)
def cal_PUC(arr):
distance_1_2 = math.sqrt(pow(arr[0][0] - arr[1][0],2) + pow(arr[0][1] - arr[1][1],2))
distance_2_3 = math.sqrt(pow(arr[1][0] - arr[2][0],2) + pow(arr[1][1] - arr[2][1],2))
distance_3_4 = math.sqrt(pow(arr[2][0] - arr[3][0],2) + pow(arr[2][1] - arr[3][1],2))
distance_4_5 = math.sqrt(pow(arr[3][0] - arr[4][0],2) + pow(arr[3][1] - arr[4][1],2))
distance_5_6 = math.sqrt(pow(arr[4][0] - arr[5][0],2) + pow(arr[4][1] - arr[5][1],2))
distance_6_1 = math.sqrt(pow(arr[5][0] - arr[0][0],2) + pow(arr[5][1] - arr[0][1],2))
distance_2_5 = math.sqrt(pow(arr[2][0] - arr[5][0],2) + pow(arr[2][1] - arr[5][1],2))
perimeter = distance_1_2 + distance_2_3 + distance_3_4 + distance_4_5 + distance_5_6 + distance_6_1
area = pow(distance_2_5 / 2,2) * math.pi
circularity = (4 * math.pi * area) / pow(perimeter,2)
return circularity
# calculate eyebrow angle (EBA)
def cal_EBA(arr):
hypotenuse = math.sqrt(pow(arr[2][0] - arr[3][0],2))
opposite = abs(arr[2][1] - arr[3][1])
angle = opposite / hypotenuse
return angle
# calculate chin aspect ratio (CAR)
def cal_CAR(arr):
p0_p16 = math.sqrt(pow(arr[0][0] - arr[16][0],2) + pow(arr[0][1] - arr[16][1],2))
p0_p8 = math.sqrt(pow(arr[0][0] - arr[8][0],2) + pow(arr[0][1] - arr[8][1],2))
CAR = p0_p8 / p0_p16
return CAR
def modify_csv(file_name):
# ask user to enter the value of the class
CLASS = input("Please enter the value of the class: ")
# open the csv file
with open(file_name,'w',newline='') as file:
writer = csv.writer(file)
writer.writerow(["L_EAR","R_EAR","MAR","PUC","MOE","ABA","CAR","CLASS"])
rows = 0
is_existed = True
while is_existed:
# input the name of the csv file
file_name = input("Please enter the name of the csv file you want to create: ")
file_name = "../csv_files/" + file_name + ".csv"
# check whether the file is already existed
is_existed = path.exists(file_name)
# warn user if the file is already existed
if is_existed:
print("The file is already existed")
# ask user whether he/she wants to delete it or create another file
choice = input("Do you want to rewrite file? (N/Y): ")
if choice.upper() == 'N':
continue
elif choice.upper() == 'Y':
is_existed = False
modify_csv(file_name)
else:
is_existed = False
modify_csv(file_name)
while True:
# turn on the webcam and check the status
capture = cv2.VideoCapture(0)
if capture.isOpened() is False:
print("Camera Error,please check your camera @_@")
exit()
# change the BGR frame to gray frame
ret,frame = capture.read()
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
# use face_recognition library to locate the landmarks
face_marks = face_recognition.face_landmarks(gray,None,"large")
print(face_marks)
print("chin points: ",face_marks[0]["chin"])
print("left_eyebrow points: ",face_marks[0]["left_eyebrow"])
print("right_eyebrow points: ",face_marks[0]["right_eyebrow"])
print("nose_bridge points: ",face_marks[0]["nose_bridge"])
print("nose_tip points: ",face_marks[0]["nose_tip"])
print("left_eye points: ",face_marks[0]["left_eye"])
print("right_eye points: ",face_marks[0]["right_eye"])
print("top_lip points: ",face_marks[0]["top_lip"])
print("bottom_lip points: ",face_marks[0]["bottom_lip"])
# calculate EAR,MAR,PUC,and MOE
if face_marks.__len__() != 0:
L_EAR = cal_EAR(face_marks[0]["left_eye"])
R_EAR = cal_EAR(face_marks[0]["right_eye"])
MAR = cal_MAR(face_marks[0]["top_lip"],face_marks[0]["bottom_lip"])
PUC = cal_PUC(face_marks[0]["left_eye"])
EBA = cal_EBA(face_marks[0]["right_eyebrow"])
CAR = cal_CAR(face_marks[0]["chin"])
MOE = MAR / L_EAR
# write these values to csv file
with open(file_name,'a',newline='') as file:
writer = csv.writer(file)
writer.writerow([L_EAR,R_EAR,MOE,EBA,CAR,CLASS])
rows += 1
print("Number of records: ",rows)
# mark these landmarks on the frame
for landmarks_dict in face_marks:
print(landmarks_dict)
point_order = 0
for landmarks_key in landmarks_dict.keys():
# keys: chin,left_eyebrow,right_eyebrow,nose_bridge,nose_tip,left_eye,right_eye,top_lip,bottom_lip
print(landmarks_key)
for point in landmarks_dict[landmarks_key]:
point_position = (point[0],point[1])
cv2.circle(frame,point,3,(255,0),-1)
cv2.putText(frame,str(point_order),point_position,fontFace=cv2.FONT_HERSHEY_SCRIPT_SIMPLEX,fontScale=0.3,color=(0,255,0))
print("Point ",point_order,"is",point_position)
point_order = point_order + 1
# rectangle the face
face_point = face_recognition.face_locations(gray)
print(face_point)
for pts in face_point:
cv2.rectangle(frame,(pts[3],pts[0]),(pts[1],pts[2]),2)
# press q to exit the loop
if rows >= 200:
print("Done")
break
# display the frame
cv2.imshow("Face_landmarks_fr",frame)
# release the memory
capture.release()
cv2.destroyAllWindows()
进行索引将其索引的元素具体化,因此 []
计算前 150 个元素的(非惰性)列表。 (列表的其余部分保持惰性,但那些初始元素不会)。
如果你想懒惰地迭代,你可以使用 [^150]
代替,它会给你以下最后一行:
&head