Ejemplo n.º 1
0
    def __init__(self, path):
        #Open the video file and loading the background image
        self.video_capture = cv2.VideoCapture(path)
        ret, background_img = self.video_capture.read()

        #Сreate an error if the video failed to load
        if not ret: Exception("No file")

        #Getting info about video: fps, width and height
        self.fps = self.video_capture.get(cv2.CAP_PROP_FPS)
        self.width = int(self.video_capture.get(cv2.CAP_PROP_FRAME_WIDTH))
        self.height = int(self.video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT))

        # Define the codec
        self.fourcc = cv2.VideoWriter_fourcc(*'mp4v')

        #Decalring the motion detector object and setting the background
        self.my_motion_detector = DiffMotionDetector()
        self.my_motion_detector.setBackground(background_img)

        #Declaring the binary mask analyser object
        self.my_mask_analyser = BinaryMaskAnalyser()

        #Declaring the controlled MotionWriter helper
        self.motion_writer = MotionWriter(self.fourcc, self.fps, self.width,
                                          self.height)
Ejemplo n.º 2
0
 def _track_movement(self, template_path, delay=0.5):
     """ Given a colour template it tracks the 
     
     @param delay: 
     @return: 
     """
     my_mask_analyser = BinaryMaskAnalyser()
     t = threading.currentThread()
     template = cv2.imread(template_path)  # Load the image
     my_back_detector = BackProjectionColorDetector()  # Defining the deepgaze color detector object
     my_back_detector.setTemplate(template)  # Set the template
     #cv2.namedWindow('filtered')
     while getattr(t, "do_run", True):
         #img_array = np.zeros((360,240,3), dtype=np.uint8)
         img_array = self.return_left_camera_image(mode='BGR')
         image_filtered = my_back_detector.returnFiltered(img_array, morph_opening=True,
                                                          blur=True, kernel_size=7, iterations=2)
         cx, cy = my_mask_analyser.returnMaxAreaCenter(image_filtered)
         if cx is not None:
             cv2.circle(image_filtered,(cx,cy), 5, (0, 0, 255), -1)
             bottle = yarp.Bottle()
             bottle.clear()
             bottle.addString('left')
             bottle.addDouble(cx)
             bottle.addDouble(cy)
             bottle.addDouble(1.0)
             self.port_ikin_mono.write(bottle)
         #images_stack = np.hstack((img_array, image_filtered))
         #cv2.imshow('filtered', images_stack)
         #cv2.waitKey(100) #waiting 50 msec
         time.sleep(0.1)
Ejemplo n.º 3
0
class VideoAnalyser:
    def __init__(self, path):
        #Open the video file and loading the background image
        self.video_capture = cv2.VideoCapture(path)
        ret, background_img = self.video_capture.read()

        #Сreate an error if the video failed to load
        if not ret: Exception("No file")

        #Getting info about video: fps, width and height
        self.fps = self.video_capture.get(cv2.CAP_PROP_FPS)
        self.width = int(self.video_capture.get(cv2.CAP_PROP_FRAME_WIDTH))
        self.height = int(self.video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT))

        # Define the codec
        self.fourcc = cv2.VideoWriter_fourcc(*'mp4v')

        #Decalring the motion detector object and setting the background
        self.my_motion_detector = DiffMotionDetector()
        self.my_motion_detector.setBackground(background_img)

        #Declaring the binary mask analyser object
        self.my_mask_analyser = BinaryMaskAnalyser()

        #Declaring the controlled MotionWriter helper
        self.motion_writer = MotionWriter(self.fourcc, self.fps, self.width,
                                          self.height)

    def analyze(self):
        print("Analysing...")
        self.motion_writer.start()

        while (True):
            # Capture frame-by-frame
            ret, frame = self.video_capture.read()
            if not ret: break

            frame_mask = self.my_motion_detector.returnMask(frame,
                                                            threshold=10)

            if (self.my_mask_analyser.returnNumberOfContours(frame_mask) > 0):
                self.my_mask_analyser.drawMaxAreaRectangle(frame, frame_mask)
                self.motion_writer.detected_motion(frame)
            else:
                self.motion_writer.undetected_motion()

        self.motion_writer.end()

    def get_all_timestaps(self):
        return self.motion_writer.get_all_timestaps()
Ejemplo n.º 4
0
 def _track_movement(self, template_path, delay=0.5):
     """ Given a colour template it tracks the 
     
     @param delay: 
     @return: 
     """
     my_mask_analyser = BinaryMaskAnalyser()
     t = threading.currentThread()
     template = cv2.imread(template_path)  # Load the image
     my_back_detector = BackProjectionColorDetector(
     )  # Defining the deepgaze color detector object
     my_back_detector.setTemplate(template)  # Set the template
     #cv2.namedWindow('filtered')
     while getattr(t, "do_run", True):
         #img_array = np.zeros((360,240,3), dtype=np.uint8)
         img_array = self.return_left_camera_image(mode='BGR')
         image_filtered = my_back_detector.returnFiltered(
             img_array,
             morph_opening=True,
             blur=True,
             kernel_size=7,
             iterations=2)
         cx, cy = my_mask_analyser.returnMaxAreaCenter(image_filtered)
         if cx is not None:
             cv2.circle(image_filtered, (cx, cy), 5, (0, 0, 255), -1)
             bottle = yarp.Bottle()
             bottle.clear()
             bottle.addString('left')
             bottle.addDouble(cx)
             bottle.addDouble(cy)
             bottle.addDouble(1.0)
             self.port_ikin_mono.write(bottle)
         #images_stack = np.hstack((img_array, image_filtered))
         #cv2.imshow('filtered', images_stack)
         #cv2.waitKey(100) #waiting 50 msec
         time.sleep(0.1)
template_list.append(cv2.imread('template_4.png'))  #Load the image
template_list.append(cv2.imread('template_5.png'))  #Load the image
template_list.append(cv2.imread('template_6.png'))  #Load the image

#Open a webcam streaming
video_capture = cv2.VideoCapture(1)  #Open the webcam
video_capture.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 320)
video_capture.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 240)
cam_w = int(video_capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH))
cam_h = int(video_capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT))
#Declare an offset that is used to define the distance
#from the webcam center of the two red lines
offset = int(cam_h / 7)

#Declaring the binary mask analyser object
my_mask_analyser = BinaryMaskAnalyser()

#Defining the deepgaze color detector object
my_back_detector = MultiBackProjectionColorDetector()
my_back_detector.setTemplateList(template_list)  #Set the template

print(
    "Welcome! Press 'a' to start the tracking of your hand. Press 'q' to exit..."
)

while (True):
    # Capture frame-by-frame
    ret, frame = video_capture.read()
    if (frame is None): break  #check for empty frames
    #Return the binary mask from the backprojection algorithm
    frame_mask = my_back_detector.returnMask(frame,
import numpy as np
import cv2
from deepgaze.motion_detection import DiffMotionDetector
from deepgaze.mask_analysis import BinaryMaskAnalyser

#Open the video file and loading the background image
video_capture = cv2.VideoCapture("./10cm.avi")
background_image = cv2.imread("./10cm.png")

#Decalring the motion detector object and setting the background
my_motion_detector = DiffMotionDetector()
my_motion_detector.setBackground(background_image)

#Declaring the binary mask analyser object
my_mask_analyser = BinaryMaskAnalyser()

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter("./10cmDeepgaze.avi", fourcc, 20.0, (600, 1062))

#Create the main window and move it
cv2.namedWindow('Video')
cv2.moveWindow('Video', 20, 20)
is_first_frame = True

while (True):

    # Capture frame-by-frame
    ret, frame = video_capture.read()
# In this example I will use the range color detector class to find a face in a picture.
# To find a face it is possible to filter the skin color and then take the center
# of the contour with the largest area. In most of the case this contour is the face.
# This example can be extended using face detector in order to check if the contour
# is a face or something else.

import numpy as np
import cv2
from deepgaze.color_detection import RangeColorDetector
from deepgaze.mask_analysis import BinaryMaskAnalyser

#Declaring the boundaries and creating the object
min_range = np.array([0, 58, 50], dtype = "uint8") #lower HSV boundary of skin color
max_range = np.array([30, 255, 255], dtype = "uint8") #upper HSV boundary of skin color
my_skin_detector = RangeColorDetector(min_range, max_range) #Define the detector object
my_mask_analyser = BinaryMaskAnalyser()

image = cv2.imread("tomb_rider_2.jpg") #Read the image with OpenCV
#For this image we use one iteration of the morph_opening and gaussian blur to clear the noise
image_filtered = my_skin_detector.returnFiltered(image, morph_opening=True, blur=True, kernel_size=3, iterations=1)
#To use the function returnMaxAreaCenter we need to have the balck and white mask
image_mask = my_skin_detector.returnMask(image, morph_opening=True, blur=True, kernel_size=3, iterations=1) 

#Here we get the center of the contour with largest area and
#the contour points.
cx, cy = my_mask_analyser.returnMaxAreaCenter(image_mask)
cnt = my_mask_analyser.returnMaxAreaContour(image_mask)

#Uncomment if you want to get the coords of the rectangle sorrounding
#the largest area contour (in this case the face)
#x, y, w, h = my_mask_analyser.returnMaxAreaRectangle(image_mask)
# This example can be extended using face detector in order to check if the contour
# is a face or something else.

import numpy as np
import cv2
from deepgaze.color_detection import RangeColorDetector
from deepgaze.mask_analysis import BinaryMaskAnalyser

#Declaring the boundaries and creating the object
min_range = np.array([0, 58, 50],
                     dtype="uint8")  #lower HSV boundary of skin color
max_range = np.array([30, 255, 255],
                     dtype="uint8")  #upper HSV boundary of skin color
my_skin_detector = RangeColorDetector(min_range,
                                      max_range)  #Define the detector object
my_mask_analyser = BinaryMaskAnalyser()

image = cv2.imread("tomb_rider_2.jpg")  #Read the image with OpenCV
#For this image we use one iteration of the morph_opening and gaussian blur to clear the noise
image_filtered = my_skin_detector.returnFiltered(image,
                                                 morph_opening=True,
                                                 blur=True,
                                                 kernel_size=3,
                                                 iterations=1)
#To use the function returnMaxAreaCenter we need to have the balck and white mask
image_mask = my_skin_detector.returnMask(image,
                                         morph_opening=True,
                                         blur=True,
                                         kernel_size=3,
                                         iterations=1)
#In this case you have to provide a valid tamplate, it can be
#a solid color you want to track or a frame containint your face.
#Substitute the frame to the default template.png.
USE_WEBCAM = False

template = cv2.imread('template.png') #Load the image
if(USE_WEBCAM == False):
    video_capture = cv2.VideoCapture("./cows.avi")
else:
    video_capture = cv2.VideoCapture(0) #Open the webcam
# Define the codec and create VideoWriter object
fourcc = cv2.cv.CV_FOURCC(*'XVID')
out = cv2.VideoWriter("./cows_output.avi", fourcc, 25.0, (1920,1080))

#Declaring the binary mask analyser object
my_mask_analyser = BinaryMaskAnalyser()

#Defining the deepgaze color detector object
my_back_detector = BackProjectionColorDetector()
my_back_detector.setTemplate(template) #Set the template 

#Filter parameters
tot_particles = 3000
#Standard deviation which represent how to spread the particles
#in the prediction phase.
std = 25 
my_particle = ParticleFilter(1920, 1080, tot_particles)
#Probability to get a faulty measurement
noise_probability = 0.15 #in range [0, 1.0]

while(True):
Ejemplo n.º 10
0
# Set filename
filename = "BlueBounce.mp4"

# Set template (to identify object, i.e. blue ball)
template = cv2.imread('BlueBounceTemplate.png')  #Load the image

# Capture video
video = cv2.VideoCapture(filename)
#video.set(1,10)

# Get video frame
video_width = video.get(cv2.CAP_PROP_FRAME_WIDTH)
video_height = video.get(cv2.CAP_PROP_FRAME_HEIGHT)

#Declaring the binary mask analyser object
my_mask_analyser = BinaryMaskAnalyser()

# Defining the deepgaze color detector object
my_back_detector = BackProjectionColorDetector()
my_back_detector.setTemplate(template)

# Filter parameters
n_particles = 500  # set number of particles

# Probability of error (a.k.a. added noise)
noise_probability = 0.015

# Spread of the particles in prediction phase
std = 25

# Initialize model within video frame
import numpy as np
import cv2
from deepgaze.motion_detection import DiffMotionDetector
from deepgaze.mask_analysis import BinaryMaskAnalyser

#Open the video file and loading the background image
video_capture = cv2.VideoCapture("./cars.avi")
background_image = cv2.imread("./background.png")

#Decalring the motion detector object and setting the background
my_motion_detector = DiffMotionDetector()
my_motion_detector.setBackground(background_image)

#Declaring the binary mask analyser object
my_mask_analyser = BinaryMaskAnalyser()

# Define the codec and create VideoWriter object
fourcc = cv2.cv.CV_FOURCC(*'XVID')
out = cv2.VideoWriter("./cars_deepgaze.avi", fourcc, 20.0, (1920,1080))

#Create the main window and move it
cv2.namedWindow('Video')
cv2.moveWindow('Video', 20, 20)
is_first_frame = True

while(True):

    # Capture frame-by-frame
    ret, frame = video_capture.read()
template_list.append(cv2.imread('template_6.png')) #Load the image

#Open a webcam streaming
video_capture=cv2.VideoCapture(0) #Open the webcam
#Reduce the size of the frame to 320x240
video_capture.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 320)
video_capture.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 240)
#Get the webcam resolution
cam_w = int(video_capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH))
cam_h = int(video_capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT))
#Declare an offset that is used to define the distance
#from the webcam center of the two red lines
offset = int(cam_h / 7)

#Declaring the binary mask analyser object
my_mask_analyser = BinaryMaskAnalyser()

#Defining the deepgaze color detector object
my_back_detector = MultiBackProjectionColorDetector()
my_back_detector.setTemplateList(template_list) #Set the template 

print("Welcome! Press 'a' to start the hand tracking. Press 'q' to exit...")

while(True):
    # Capture frame-by-frame
    ret, frame = video_capture.read()
    if(frame is None): break #check for empty frames
    #Return the binary mask from the backprojection algorithm
    frame_mask = my_back_detector.returnMask(frame, morph_opening=True, blur=True, kernel_size=5, iterations=2)
    if(my_mask_analyser.returnNumberOfContours(frame_mask) > 0 and ENABLE_CAPTURE==True):
        x_center, y_center = my_mask_analyser.returnMaxAreaCenter(frame_mask)