search
HomeTechnology peripheralsAIAI ban on smoking is okay! Smoking recognition + face recognition

Hello, everyone.

Today I will share with you a smoking recognition and face recognition project. Many public places, production sites and schools have bans on smoking. It is still necessary to implement a ban on smoking and let AI automatically identify smoking behavior and identify who is smoking.

Use the target detection algorithm to determine smoking behavior, extract the face of the smoker, and use the face recognition algorithm to determine who is smoking. The idea is relatively simple, but the details are still a little troublesome.

The training data and source code used in the project have been packaged. It’s still the same as before, get it in the comment section.

1. Detecting cigarettes

I used 5k pieces of labeled smoking data as training data

AI ban on smoking is okay! Smoking recognition + face recognition

and placed them in the dataset directory.

Train YOLOv5 target detection model.

First step, copy data/coco128.yaml to smoke.yaml, and modify the data set directory and category configuration information

path: ../dataset/smoke # dataset root dir
train: images/train# train images (relative to 'path') 128 images
val: images/test# val images (relative to 'path') 128 images
test:# test images (optional)

# Classes
names:
0: smoke

Second step, copy ./models/yolov5s.yaml to smoke.yaml​, modify nc

nc: 1# number of classes

The third step is to download the yolov5s.pt pre-trained model and place it in the {yolov5 directory}/weights directory

Execute the following command to train.

python ./train.py --data ./data/smoke.yaml --cfg ./models/smoke.yaml --weights ./weights/yolov5s.pt --batch-size 30 --epochs 120 --workers 8 --name smoke --project smoke_s

AI ban on smoking is okay! Smoking recognition + face recognition

After the training is completed, you can see the following output:

AI ban on smoking is okay! Smoking recognition + face recognition

The call is just fine.

After the training is completed, the best.pt position can be found and used later for cigarette detection.

model = torch.hub.load('../28_people_counting/yolov5', 'custom', './weights/ciga.pt', source='local')

results = self.model(img[:, :, ::-1])
pd = results.pandas().xyxy[0]
ciga_pd = pd[pd['class'] == 0]

AI ban on smoking is okay! Smoking recognition + face recognition

After being able to identify cigarettes, we still need to determine whether we are currently smoking.

You can use the cigarette detection frame and the mouth detection frame to calculate the IOU to determine. To put it bluntly, it is to determine whether the two frames intersect. If so, it is considered that you are currently smoking.

AI ban on smoking is okay! Smoking recognition + face recognition

Mouth detection frame, using facial key points to identify.

2. Face recognition

There are many mature models for face recognition algorithms. We don’t need to train them ourselves, we can just adjust the database directly.

I am using the dlib library here, which can identify 68 key points on a face and extract facial features based on these 68 key points.

AI ban on smoking is okay! Smoking recognition + face recognition

face_detector = dlib.get_frontal_face_detector()
face_sp = dlib.shape_predictor('./weights/shape_predictor_68_face_landmarks.dat')

dets = face_detector(img, 1)

face_list = []
for face in dets:
l, t, r, b = face.left(), face.top(), face.right(), face.bottom()

face_shape = face_sp(img, face)

face_detector​can detect faces and return face detection frames. face_sp is based on face detection frames and identifies 68 key points of faces.

From these 68 key points, we can obtain the mouth detection frame to determine whether you are smoking.

Finally, we still hope to use face recognition algorithms to identify who is smoking.

The first step is to extract facial features

face_feature_model = dlib.face_recognition_model_v1('./weights/dlib_face_recognition_resnet_model_v1.dat')

face_descriptor = face_feature_model.compute_face_descriptor(img, face_shape)

face_descriptor​Calculate a feature vector for each face based on the position and distance between the 68 key points of the face. This principle is similar to the word2vec we shared before or mapping videos to N-dimensional vectors.

The second step is to enter the existing faces into the face database. I prepared 3 smoking behaviors in movies and TV series

AI ban on smoking is okay! Smoking recognition + face recognition

Cut faces from the videos, vectorize them, and write them into the face database (replaced with files)

AI ban on smoking is okay! Smoking recognition + face recognition

The third step, after smoking occurs, we can crop out the smoker’s face, calculate the face vector, compare it with the features of the face database, and find the best Similar faces, return the corresponding name

AI ban on smoking is okay! Smoking recognition + face recognition

def find_face_name(self, face_feat):
"""
人脸识别,计算吸烟者名称
:param face_feat:
:return:
"""
cur_face_feature = np.asarray(face_feat, dtype=np.float64).reshape((1, -1))

# 计算两个向量(两张脸)余弦相似度
distances = np.linalg.norm((cur_face_feature - self.face_feats), axis=1)
min_dist_index = np.argmin(distances)
min_dist = distances[min_dist_index]

if min_dist < 0.3:
return self.face_name_list[min_dist_index]
else:
return '未知'

There are many areas where this project can be expanded, for example: the video I provided only has a single face, and it will definitely be used in actual monitoring. It’s multiple faces. At this time, the MOT algorithm can be used to track pedestrians, and then each person can be individually identified for smoking

Also, a separate statistical area can be created to save the identified smoking behaviors and use them as evidence for warnings and punishments .

The above is the detailed content of AI ban on smoking is okay! Smoking recognition + face recognition. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:51CTO.COM. If there is any infringement, please contact admin@php.cn delete
A Comprehensive Guide to ExtrapolationA Comprehensive Guide to ExtrapolationApr 15, 2025 am 11:38 AM

Introduction Suppose there is a farmer who daily observes the progress of crops in several weeks. He looks at the growth rates and begins to ponder about how much more taller his plants could grow in another few weeks. From th

The Rise Of Soft AI And What It Means For Businesses TodayThe Rise Of Soft AI And What It Means For Businesses TodayApr 15, 2025 am 11:36 AM

Soft AI — defined as AI systems designed to perform specific, narrow tasks using approximate reasoning, pattern recognition, and flexible decision-making — seeks to mimic human-like thinking by embracing ambiguity. But what does this mean for busine

Evolving Security Frameworks For The AI FrontierEvolving Security Frameworks For The AI FrontierApr 15, 2025 am 11:34 AM

The answer is clear—just as cloud computing required a shift toward cloud-native security tools, AI demands a new breed of security solutions designed specifically for AI's unique needs. The Rise of Cloud Computing and Security Lessons Learned In th

3 Ways Generative AI Amplifies Entrepreneurs: Beware Of Averages!3 Ways Generative AI Amplifies Entrepreneurs: Beware Of Averages!Apr 15, 2025 am 11:33 AM

Entrepreneurs and using AI and Generative AI to make their businesses better. At the same time, it is important to remember generative AI, like all technologies, is an amplifier – making the good great and the mediocre, worse. A rigorous 2024 study o

New Short Course on Embedding Models by Andrew NgNew Short Course on Embedding Models by Andrew NgApr 15, 2025 am 11:32 AM

Unlock the Power of Embedding Models: A Deep Dive into Andrew Ng's New Course Imagine a future where machines understand and respond to your questions with perfect accuracy. This isn't science fiction; thanks to advancements in AI, it's becoming a r

Is Hallucination in Large Language Models (LLMs) Inevitable?Is Hallucination in Large Language Models (LLMs) Inevitable?Apr 15, 2025 am 11:31 AM

Large Language Models (LLMs) and the Inevitable Problem of Hallucinations You've likely used AI models like ChatGPT, Claude, and Gemini. These are all examples of Large Language Models (LLMs), powerful AI systems trained on massive text datasets to

The 60% Problem — How AI Search Is Draining Your TrafficThe 60% Problem — How AI Search Is Draining Your TrafficApr 15, 2025 am 11:28 AM

Recent research has shown that AI Overviews can cause a whopping 15-64% decline in organic traffic, based on industry and search type. This radical change is causing marketers to reconsider their whole strategy regarding digital visibility. The New

MIT Media Lab To Put Human Flourishing At The Heart Of AI R&DMIT Media Lab To Put Human Flourishing At The Heart Of AI R&DApr 15, 2025 am 11:26 AM

A recent report from Elon University’s Imagining The Digital Future Center surveyed nearly 300 global technology experts. The resulting report, ‘Being Human in 2035’, concluded that most are concerned that the deepening adoption of AI systems over t

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor