Drawing Virtual Shapes on Live video through mouse click using Python+openCV+mySQL(Raspberry pi 3B+)
In this OpenCV Python Tutorial blog, I will be covering various aspects of Computer Vision using OpenCV in Python. OpenCV provides many drawing functions to draw geometric shapes and write text on images, But drawing virtual geometric shapes using Mouse click become more easier and its also dost not need any fix 3D coordinates to draw shape.
In this blog i will describe how to draw virtual shapes on live video frame. here i am using raspberry pi 3b+ as processor and pycam to capture live video.
Whole flow divided in to three step. in 1st step i initialize all require libraries for live video capture, creating table in mysql , inserting the coordinates of each mouse click and after import every coordinate. 2nd step code describe how to draw virtual line by using last 2 coordinate of mouse click.and in last step my code says that how any particular closed geometric virtual shape is draw by each next mouse click.
Highlight: For this program one should have to install mysql with phpmyadmin .this tutorial also provide on my blog site : https://aiworldofjuhi.blogspot.com/2019/02/how-to-install-mysql-on-raspberry-pi-3b.html
Now, let do it
code:1 shape.py
import numpy as np #import numpy
import cv2 #import cv2
import cv2.cv as cv
from picamera.array import PiRGBArray #import picamera
from picamera import PiCamera
import time
import mysql.connector
from func import cor
from polyline import poly
#from live import drawlines
#import array as arr
from array import array
camera = PiCamera() #Picamera ready to capture
camera.resolution = (640, 480) #frameresolution(height,width)
camera.framerate = 32 #frame rate is 32 frame per sec
rawCapture = PiRGBArray(camera, size=(640, 480)) # capture raw of color frame
refpt2='' #blank variable
time.sleep(0.1) #0.1 sec time sleep
mylist=[]
L=''
mydb=mysql.connector.connect(host='localhost',
user='raspberry',passwd='juhi',database='chanchala',buffered = True)
#connect with mysql data base
mycursor=mydb.cursor()
def on_mouse(event, x, y, flags, params,):
if event == cv.CV_EVENT_LBUTTONDOWN:
refpt=[x,y]
mycursor.execute("SELECT xcor,ycor FROM juhu order by line desc limit 1")
#select data from data base
myresult=mycursor.fetchone()
A=(myresult)
sqlFormula="INSERT INTO juhu (xcor,ycor) VALUES (%s,%s)"
mycursor.execute(sqlFormula,refpt)# store data into A variable
print ("A=",A)
L=len(A)
print("l",L)
if (L==2):
mylist.append(A)
mylist.extend([A])
print ("mylist",mylist)
sqlFormula="INSERT INTO juhu (xcor,ycor) VALUES (%s,%s)"
mycursor.execute(sqlFormula,refpt)
else:
print("L is more")
elif event == cv.CV_EVENT_LBUTTONUP: #else button click is up
refpt3 = [x, y]#store new coor in refpt3 after button up
print ("point up ",refpt3)#prinr data
print 'End Mouse Position: '+str(x)+', '+str(y)
Image of inserting mouse click coordinate in mysql
Next step is to import the coordinate of mouse click and using them draw the virtual line and continuously in the end close shape.
import numpy as np #import numpy
import cv2 #import cv2
import cv2.cv as cv
from picamera.array import PiRGBArray #import picamera
from picamera import PiCamera
import time
import mysql.connector
from func import cor
from polyline import poly
#from live import drawlines
#import array as arr
from array import array
camera = PiCamera() #Picamera ready to capture
camera.resolution = (640, 480) #frameresolution(height,width)
camera.framerate = 32 #frame rate is 32 frame per sec
rawCapture = PiRGBArray(camera, size=(640, 480)) # capture raw of color frame
refpt2='' #blank variable
time.sleep(0.1) #0.1 sec time sleep
mylist=[]
L=''
mydb=mysql.connector.connect(host='localhost',
user='raspberry',passwd='juhi',database='chanchala',buffered = True)
#connect with mysql data base
mycursor=mydb.cursor()
def on_mouse(event, x, y, flags, params,):
if event == cv.CV_EVENT_LBUTTONDOWN:
refpt=[x,y]
mycursor.execute("SELECT xcor,ycor FROM juhu order by line desc limit 1")
#select data from data base
myresult=mycursor.fetchone()
A=(myresult)
sqlFormula="INSERT INTO juhu (xcor,ycor) VALUES (%s,%s)"
mycursor.execute(sqlFormula,refpt)# store data into A variable
print ("A=",A)
L=len(A)
print("l",L)
if (L==2):
mylist.append(A)
mylist.extend([A])
print ("mylist",mylist)
sqlFormula="INSERT INTO juhu (xcor,ycor) VALUES (%s,%s)"
mycursor.execute(sqlFormula,refpt)
else:
print("L is more")
elif event == cv.CV_EVENT_LBUTTONUP: #else button click is up
refpt3 = [x, y]#store new coor in refpt3 after button up
print ("point up ",refpt3)#prinr data
print 'End Mouse Position: '+str(x)+', '+str(y)
Image of inserting mouse click coordinate in mysql
Next step is to import the coordinate of mouse click and using them draw the virtual line and continuously in the end close shape.
while(1):
for frame in camera.capture_continuous(rawCapture, format="bgr",use_video_port=True):
#capture frame continuously from raw capture
image = frame.array #make frame aaray wich is us image
#img = cv2.blur(image, (3,3))
cv2.namedWindow('real image')
cv.SetMouseCallback('real image', on_mouse, 0) #mouse call back
poly(image,mylist,L)
#print("old"",arr1)
#print("new",arr2)
# draw line using arr1 and arr2
#D= drawlines(arr1,arr2,image,mycursor)
cv2.imshow('real image', image) #show the imGe
key = cv2.waitKey(1) & 0xFF
rawCapture.truncate(0)#top the raw capture
if key == ord("q"):
break
#count = 0
mydb.commit()
code:2 polyline.py
import cv2
import numpy as np
def poly(image,mylist,L)
vrx = np.array((mylist), np.int32)
vrx = vrx.reshape((-1,1,2))
cv2.polylines(image, [vrx], False, (0,255,255),3)
for frame in camera.capture_continuous(rawCapture, format="bgr",use_video_port=True):
#capture frame continuously from raw capture
image = frame.array #make frame aaray wich is us image
#img = cv2.blur(image, (3,3))
cv2.namedWindow('real image')
cv.SetMouseCallback('real image', on_mouse, 0) #mouse call back
poly(image,mylist,L)
#print("old"",arr1)
#print("new",arr2)
# draw line using arr1 and arr2
#D= drawlines(arr1,arr2,image,mycursor)
cv2.imshow('real image', image) #show the imGe
key = cv2.waitKey(1) & 0xFF
rawCapture.truncate(0)#top the raw capture
if key == ord("q"):
break
#count = 0
mydb.commit()
code:2 polyline.py
import cv2
import numpy as np
def poly(image,mylist,L)
vrx = np.array((mylist), np.int32)
vrx = vrx.reshape((-1,1,2))
cv2.polylines(image, [vrx], False, (0,255,255),3)
Result
Virtual line on live video using 2 coordinate of mouse click
Virtual close shape on live video using each coordinate of mouse click
This kind of tutorial can use in development of artificial intelligence application for example Road lane recognition system for driver-less car, real time person counting system, car counting system, artificial house hold intelligent system, Automatic staircase lightning system and also in large scale production counting system.
Need any help then comment in below section.bye python programmers .
see you in next blog.
Need any help then comment in below section.bye python programmers .
see you in next blog.
Comments
Post a Comment