background
- We will study with the aim of recognizing arbitrary patterns in videos.
- For face recognition of major images, there are DeepFace developed by Facebook and cloud-based face recognition API (!) Https://www.skybiometry.com/ provided by Sky Biometry. , This article is for people who want to move their hands more and understand the technology.
Learning material
- OpenCV: CV stands for Computer Vision. An image recognition library started by Gary Bradsky, who was at Intel at the time. It seems that you can use more than hundreds of image processing algorithms __. OpenCV 2.x series seems to be built on C ++.
- Python 2.7: Because it was on my Mac. Also, it was a language with a lot of OpenCV documentation, and it seemed easy for me (I'm doing Ruby).
- ffmpeg: Encodes necessary for OpenCV to process videos. It is not essential to touch OpenCV.
How to proceed with learning
- Pinch and eat what is written at http://docs.opencv.org/trunk/doc/py_tutorials/py_tutorials.html.
How to install OpenCV (2014/5) * Mac OS X, Mountain Lion *
- Basically, with Homebrew.
- Erase the warning that appears with brew doctor
- brew tap homebrew/science
- brew install opencv --with-ffmpeg
- export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH
- Success if import cv passes in python console
http://www.jeffreythompson.org/blog/2013/08/22/update-installing-opencv-on-mac-mountain-lion/ Reference
Video playback
- In the sample below, each frame is played while being converted to grayscale.
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()
- __ In my environment, I was angry that it wasn't a full pass __
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
- Give the initial coordinates of the area you want to track in the image
- Think of the area as a set of characterizing points (so image processing is important)
- Think of any frame
- Calculate the center of the current area
- Calculate the centroid of the set of feature points to count in the current area
- If the center of gravity of the feature point does not match the center of the area, move the area so that the center of gravity and the center of gravity match.
- Repeat this and think about the position of the object that can actually place the position where the center of gravity and the center match (with a sufficiently small error) in the current frame.
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()