Change language

Driving a car using hand detection in Python

| | | |

In this project, we are going to demonstrate how you can drive a car simply by detecting gestures on the steering wheel. Let’s say a requirement is something like this —

  • If the driver wants to start the car, put both hands on the steering wheel.
  • If someone who has no hands on the steering wheel, it means that the vehicle’s brakes will be applied slowly.
  • If one hand is found on the steering wheel, it means that he / she can drive the vehicle up to a certain limit for safety reasons.
  • If someone keeps both hands on the steering wheel, it means that he / she can move at any speed, because according to our system you are safe and can safely drive with both hands.

For this project we need to import two Python libraries —  OpenCV and numpy . How to install these two libraries.

 1) pip install opencv-python 2) pip install numpy 

Below is the implementation:

Code # 1:

import cv2

import numpy as np

 

cap = cv2.VideoCapture ( 0 )

hand_cascade = cv2.CascadeClassifier ( ’hand.xml’ )

Explanation:
We imported two libraries named opencv and numpy . Then in the next line we use the VideoCapture (0) opencv function and the VideoCapture (0) parameter as 0 because your laptop’s webcam supports port 0 for using the camera. Now use the CascadeClassifier (& # 39; hand.xml & # 39;) function and pass the xml file as a parameter. Save the hand.xml file in the same directory as the Python file.

Code # 2:

count = 0

 

while ( True ):

ret, frame = cap.read ()

gray = cv2.cvtColor (frame, cv2.COLOR_BGR2GRAY)

hands = hand_cascade.detectMultiScale (gray, 1.5 , 2 )

contour = hands

contour = np.array (contour)

 

if count = = 0 :

  

  if len (contour) = = 2 :

  cv2.puttext-method/">puttext-method/">putText (img = frame , text = ’Your engine started’ ,

org = ( int ( 100 / 2 - 20 ), int ( 100 / 2 )),

fontFace = cv2.FONT_HERSHEY_DUPLEX , 

  fontScale = 1 , color = ( 0 , 255 , 0 ))

 

for (x, y, w, h) in hands:

cv2.rectangle-method/">rectangle (frame , (x, y), (x + w, y + h), ( 0 , 255 , 0 ), 2 )

count + = 1

  

  if count" 0 :

 

if len (contour)" = 2 :

cv2.puttext-method/">puttext-method/">putText (img = frame, text = ’You can take your car on long drive’ ,

org = ( int ( 100 / 2 - 20 ), int ( 100 / 2 ) ),

fontFace = cv2.FONT_HERSHEY_DUPLEX, 

fontScale = 1 , color = ( 255 , 0 , 0 ))

 

for (x, y, w, h) in hands:

cv2.rectangle-method/">rectangle (frame, (x, y), (x + w, y + h), ( 0 , 255 , 0 ), 2 )

 

 

elif len (contour) = = 1 :

cv2.puttext-method/">puttext-method/">putText (img = frame , text = ’You can speed upto 80km / h’ ,

org = ( int ( 100 / 2 - 20 ), int ( 100 / 2 )),

  fontFace = cv2.FONT_HERSHEY_DUPLEX, 

fontScale = 1 , color = ( 0 , 255 , 0 ))

 

for (x, y, w, h) in hands:

cv2.rectangle-method/">rectangle (frame, (x, y), (x + w, y + h), ( 0 , 255 , 0 ), 2 )

 

elif len (contour) = = 0 :

cv2.puttext-method/">puttext-method/">putText (img = frame, text = ’Brake is applied slowly’ ,

  org = ( int ( 100 / 2 - 20 ), int ( 100 / 2 )),

fontFace = cv2.FONT_HERSHEY_DUPLEX, 

fontScale = 1 , color = ( 0 , 0 , 255 ))

  

  count + = 1

 

cv2.imshow ( ’ Driver_frame’ , frame)

k = cv2.waitKey ( 30 ) & amp;  0xff

if k = = 27 :

break

Output:

Explanation:
In this section of code we use a counter that can help us start the car engine, and after starting car we use contour counting on the steering wheel.

GitHub project link — Click here

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically