当前位置:网站首页>Face detection and tracking based on Dlib database
Face detection and tracking based on Dlib database
2022-07-08 02:21:00 【So come on】
1、 be based on dlib library —— Face detection 、 Track face , Add two auxiliary functions
1、 Add function 1 : Save the video
2、 Add function 2 : message cv.putText() Function application
The algorithm is still relatively old , Easy to identify errors .
Identify errors
# The algorithm is still relatively old , Easy to identify errors .
# 1 Add to Library
import cv2
import dlib
# Add function 2 : message cv.putText() Function application
def show_info(frame, tracking_state):
# Two anchor points , Write the corresponding content at the point .
pos1 = (20, 40)
pos2 = (20, 80)
cv2.putText(frame, "'1' : reset ", pos1, cv2.FONT_HERSHEY_COMPLEX, 0.5, (255,255,255)) # The last few parameters are about fonts .
# According to the State , Display different information
if tracking_state is True:
cv2.putText(frame, "tracking now ...", pos2, cv2.FONT_HERSHEY_COMPLEX, 0.5, (255, 0, 0))
else:
cv2.putText(frame, "no tracking ...", pos2, cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 255, 0))
# 2 The main function
def main():
# 3 Turn on the camera
capture = cv2.VideoCapture(0)
# 4 be based on dlib Library get face detector
detector = dlib.get_frontal_face_detector()
# 5 be based on dlib Library real-time tracking
tractor = dlib.correlation_tracker()
# 6 Tracking status
tracking_state = False
# Add function 1 : Save the video
# frame_width = capture.get(cv2.CAP_PROP_FRAME_WIDTH)
w = int(capture.get(3)) # Get video of width
# frame_height = capture.get(cv2.CAP_PROP_FRAME_HEIGHT)
h = int(capture.get(4)) # Get video of height
# frame_fps = capture.get(cv2.CAP_PROP_FPS) # There are mistakes , I still don't understand
# # Format the video
fourcc = cv2.VideoWriter_fourcc(*"XVID")
# output = cv2.VideoWriter('record.avi', fourcc, 30.0, (int(frame_width), int(frame_height)), True) # It means color , Sure
# output = cv2.VideoWriter('record.avi', fourcc, int(frame_fps), (int(frame_width), int(frame_height)), True) # Output error
# output= cv2.VideoWriter(args.video_output, fourcc, int(frame_fps), (int(frame_width), int(frame_height)), False) # Output error
output = cv2.VideoWriter(r"C:\Users\xiaohao\Desktop\record.mp4", fourcc, 15.0, (w, h))
# 7 Cycle through every frame
while True:
ret, frame = capture.read()
# Display prompt message , In circulation .
show_info(frame, tracking_state)
# 8 If there is no tracking , Start the tracker
if tracking_state is False:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
dets = detector(gray, 1) # Return the detected face
if len(dets) > 0:
tractor.start_track(frame, dets[0])
tracking_state = True
# 9 Tracking , Get the position of the face in real time , Show
if tracking_state is True:
tractor.update(frame) # Follow the new picture
position = tractor.get_position() # Get face coordinates
cv2.rectangle(frame,(int(position.left()), int(position.top())), (int(position.right()), int(position.bottom())), (0,255,0), 3)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
# Resume reset
if key == ord('1'):
tracking_state = False
cv2.imshow("face tracking", frame)
# Save the video
output.write(frame)
capture.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()
2、 Running error
output = cv2.VideoWriter("record.mp4", fourcc, int(frame_fps), (int(frame_width), int(frame_height)), True)
SystemError: <class 'cv2.VideoWriter'> returned a result with an error set
The length and width of the picture can be obtained normally (int(frame_width), int(frame_height)), The frame value is not determined int(frame_fps), You can input data by yourself . The specific reason , I'm eager to leave a message for help .
边栏推荐
- Deep understanding of softmax
- Literature reading and writing
- leetcode 869. Reordered Power of 2 | 869. Reorder to a power of 2 (state compression)
- XXL job of distributed timed tasks
- Leetcode question brushing record | 485_ Maximum number of consecutive ones
- Kwai applet guaranteed payment PHP source code packaging
- 文盘Rust -- 给程序加个日志
- Talk about the cloud deployment of local projects created by SAP IRPA studio
- Yolov5 lite: how to speed up the Yolo model on CPU?
- [knowledge atlas paper] minerva: use reinforcement learning to infer paths in the knowledge base
猜你喜欢
Master go game through deep neural network and tree search
Yolov5 lite: how to speed up the Yolo model on CPU?
leetcode 869. Reordered Power of 2 | 869. Reorder to a power of 2 (state compression)
Kwai applet guaranteed payment PHP source code packaging
《通信软件开发与应用》课程结业报告
Learn face detection from scratch: retinaface (including magic modified ghostnet+mbv2)
Semantic segmentation | learning record (2) transpose convolution
Deep understanding of cross entropy loss function
Leetcode question brushing record | 27_ Removing Elements
Popular science | what is soul binding token SBT? What is the value?
随机推荐
[recommendation system paper reading] recommendation simulation user feedback based on Reinforcement Learning
metasploit
常见的磁盘格式以及它们之间的区别
How to use diffusion models for interpolation—— Principle analysis and code practice
Monthly observation of internet medical field in May 2022
生命的高度
Anan's judgment
leetcode 866. Prime Palindrome | 866. prime palindromes
Learn face detection from scratch: retinaface (including magic modified ghostnet+mbv2)
Coreldraw2022 download and install computer system requirements technical specifications
[knowledge map paper] r2d2: knowledge map reasoning based on debate dynamics
Gaussian filtering and bilateral filtering principle, matlab implementation and result comparison
Beaucoup d'enfants ne savent pas grand - chose sur le principe sous - jacent du cadre orm, non, ice River vous emmène 10 minutes à la main "un cadre orm minimaliste" (collectionnez - le maintenant)
Leetcode question brushing record | 485_ Maximum number of consecutive ones
入侵检测——Uniscan
EMQX 5.0 发布:单集群支持 1 亿 MQTT 连接的开源物联网消息服务器
Talk about the realization of authority control and transaction record function of SAP system
文盘Rust -- 给程序加个日志
JVM memory and garbage collection-3-runtime data area / method area
[knowledge map paper] Devine: a generative anti imitation learning framework for knowledge map reasoning