[PYTHON] Pattern recognition learning in video Part 1 Field of Pattern Recognition

background

Learning material

How to proceed with learning

How to install OpenCV (2014/5) * Mac OS X, Mountain Lion *

http://www.jeffreythompson.org/blog/2013/08/22/update-installing-opencv-on-mac-mountain-lion/ Reference

Video playback

import numpy as np
import cv2

cap = cv2.VideoCapture('Full path to video')

while(cap.isOpened()):
    ret, frame = cap.read()

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

    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

http://docs.opencv.org/trunk/doc/py_tutorials/py_gui/py_video_display/py_video_display.html#display-video

Object tracking in video

MeanShift algorithm

Current source code

import numpy as np
import cv2

cap = cv2.VideoCapture('Full path to video')

ret,frame = cap.read()

r,h,c,w = 150,90,650,125
track_window = (c,r,w,h)

roi = frame[r:r+h, c:c+w]
hsv_roi = cv2.cvtColor(roi,cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv_roi, np.array((0.,60.,32.)), np.array((180.,255.,255.)))
roi_hist = cv2.calcHist([hsv_roi],[0],mask,[180],[0,180])
cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX)


term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )

while(1):
  ret,frame = cap.read()

  if ret == True:
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,180],1)

    ret,track_window = cv2.meanShift(dst, track_window, term_crit)

    x,y,w,h = track_window
    img2 = cv2.rectangle(frame,(x,y),(x+w,y+h),255,2)
    cv2.imshow('img2',frame)

    k = cv2.waitKey(60) & 0xff
    if k == 27:
      break
    else:
      cv2.imwrite(chr(k)+".jpg ",img2)

  else:
    break

cv2.destroyAllWindows()
cap.release()

Recommended Posts

Pattern recognition learning in video Part 1 Field of Pattern Recognition
[Gang of Four] Design pattern learning
Chain of Responsibility pattern in Java
[Gang of Four] Design pattern learning --Singleton
[Gang of Four] Design Pattern Learning --Decorator
Delusion of 3D from 2D video [Unsupervised Monocular Depth Learning in Dynamic Scenes]
[Gang of Four] Design pattern learning --Mediator
[Gang of Four] Design pattern learning --Iterator
Implement part of the process in C ++
Image recognition model using deep learning in 2016
[Gang of Four] Design pattern learning --Facade
[Gang of Four] Design pattern learning --Composite
[Gang of Four] Design pattern learning --Prototype
[Gang of Four] Design pattern learning --Memento
Traffic Safety-kun: Recognition of traffic signs in Python
[Gang of Four] Design pattern learning --State
[Gang of Four] Design pattern learning --Interpreter
[Gang of Four] Design pattern learning --Builder
[Gang of Four] Design pattern learning --Bridge
[Gang of Four] Design pattern learning --Proxy
[Gang of Four] Design pattern learning --Strategy
[Gang of Four] Design pattern learning --Adapter
[Gang of Four] Design pattern learning --Observer
[Gang of Four] Design pattern learning --Command
Machine learning memo of a fledgling engineer Part 1
Classification of guitar images by machine learning Part 1
[Gang of Four] Design pattern learning --Abstract Factory
Basics of Supervised Learning Part 1-Simple Regression- (Note)
[Gang of Four] Design pattern learning --Factory Method
Full disclosure of methods used in machine learning
Video frame interpolation by deep learning Part1 [Python]
Getting rid of DICOM images in Python Part 2
[Gang of Four] Design pattern learning --Chain of Responsibility
Summary of evaluation functions used in machine learning
Machine learning memo of a fledgling engineer Part 2
Classification of guitar images by machine learning Part 2
Get a glimpse of machine learning in Python
Implementation of Deep Learning model for image recognition
[Gang of Four] Design pattern learning --Template Method
Basics of Supervised Learning Part 3-Multiple Regression (Implementation)-(Notes)-
Report_Deep Learning (Part 1)
Report_Deep Learning (Part 1)
Report_Deep Learning (Part 2)
Count the number of parameters in the deep learning model
About testing in the implementation of machine learning models
Learn the design pattern "Chain of Responsibility" in Python