SoFunction
Updated on 2024-11-14

Yawn detection based on Python implementation details

Yawn detection based on Python implementation details

Updated April 12, 2022 08:34:39 by Kawakawa Rookie
This article mainly introduces how to use Python + OpenCV to achieve human yawn detection, the sample code in the text explains the details of our study of Python has a certain degree of help, interested in learning about it!

rendering (visual representation of how things will turn out)

basic idea

  • Initialize a video rendering object in OpenCV using the VideoCapture method
  • Creating grayscale images
  • Importing pre-trained models to recognize faces and face signs
  • Calculate the distance between the upper and lower lip (other similar)
  • Create an If condition for the lip distance that is satisfied by yawning and not satisfied by simply opening the mouth
  • Display frames/images

Partial source code

  suc, frame = ()
    # Exit without reading
    if not suc:
        break

    # ---------FPS------------#
    ctime = ()
    fps = int(1 / (ctime - ptime))
    ptime = ctime
    (frame, f'FPS:{fps}', ([1] - 120, [0] - 20), cv2.FONT_HERSHEY_PLAIN, 2,
                (0, 200, 0), 3)

    # ------ detecting faces ------#
    # To grayscale
    img_gray = (frame, cv2.COLOR_BGR2GRAY)
    faces = face_model(img_gray)
    for face in faces:
        # Detect the face, frame it up-#
        x1 = ()
        y1 = ()
        x2 = ()
        y2 = ()
        # print(())
        (frame, (x1, y1), (x2, y2), (200, 0, 00), 2)

        # ---------- detecting face annotation -----------#
        shapes = landmark_model(img_gray, face)
        shape = face_utils.shape_to_np(shapes)

        # ------- detecting upper and lower lips --------#
        lip = shape[48:60]
        (frame, [lip], -1, (0, 165, 255), thickness=3)

        # ------- Calculate the distance between the upper and lower lip -----#
        lip_dist = cal_yawn(shape)
        # Print distance
        # print(lip_dist)
        # Greater than the set value, it is recognized as yawning
        if lip_dist > yawn_thresh:
            (frame, f'User Yawning!', ([1] // 2 - 170, [0] // 2),
                        cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 200), 2)

    # Press the letter q to exit
    ('Webcam', frame)
    if (1) & 0xFF == ord('q'):
        break

This article on the realization of yawn detection based on Python detailed article is introduced to this, more related Python yawn detection content, please search for my previous posts or continue to browse the following related articles I hope that you will support me in the future more!

  • Python
  • yawn
  • sensing

Related articles

  • Example of how to use python multithreading in detail

    This article introduces the use of python multithreading, combined with examples of the form of detailed analysis of the Python multithread thread module, locking mechanism related to the use of skills and operational considerations, the need for friends can refer to the following
    2019-12-12
  • How to add a legend using () in python Code Example

    matplotlib drawing with python, often need to add the legend, the following article introduces how to use python () to add the legend of the relevant information, the text of the code through the introduction of the very detailed, the need for friends can refer to the next!
    2023-11-11
  • Python Output Chess Board Example Sharing

    In this article, I am going to give you an example of python output chess board, interested friends can refer to the following.
    2020-11-11
  • python batch search files by filename

    Hello, this post is about python according to the file name batch search files, interested students quickly take a look at it, to help you remember to collect it
    2022-01-01
  • Django Model Serialization Returns Natural Primary Key Value Sample Code

    This article introduces you to the Django model serialization returns the natural primary key value of the relevant information, the text through the sample code is very detailed, for everyone to learn or use Django has a certain reference learning value, the need for friends below to learn together to learn it!
    2019-06-06
  • Python Class Methods Summary Explained

    This article introduces the Python class methods summarize the explanation, this article through a brief case, explains the understanding and use of the technology, the following is the details, the need for friends can refer to the following
    2021-07-07
  • Python development of HTTP library requests details

    Requests is an HTTP library written in Python, based on urllib and using the Apache2 Licensed open source protocol. It is more convenient than urllib , can save us a lot of work , fully meet the needs of HTTP testing.Requests philosophy is centered on the development of PEP 20 idiom , so it is more Pythoner than urllib. more important point is that it supports Python3 oh!
    2017-08-08
  • Python PyMuPDF to achieve PDF and images and PPT conversion each other

    can be used to manipulate the PDF document Python package there are several, such as extracting the contents of PdfPlumber, PDFMiner, can be used to modify the PDF file operation PyPDF2 and so on, if you simply need to achieve the merger of PDF files, split, bookmarks, the use of PyPDF2 will be enough to meet. But if you want to carry out some underlying operations on PDF files, basically only PyMuPDF!
    2022-12-12
  • Basic use of python decorators

    This article introduces the basic use of python decorator, to help you better understand and use python, interested parties can understand the following
    2021-01-01
  • Python xml and dict conversion method details

    This article introduces the Python xml and dict conversion method explained, xml refers to the extensible markup language, a markup language similar to html, the role of the transmission of data, and is not to display data. You can customize the label, if you need to refer to the next friend.
    2023-07-07

Latest Comments