def __init__(self,bbox,img):
self.tracker = correlation_tracker()
self.tracker.start_track(img,rectangle(long(bbox[0]),long(bbox[1]),long(bbox[2]),long(bbox[3])))
self.confidence = 0. # measures how confident the tracker is! (a.k.a. correlation score)
self.time_since_update = 0
self.id = CorrelationTracker.count
CorrelationTracker.count += 1
self.hits = 0
self.hit_streak = 0
self.age = 0
python类rectangle()的实例源码
def update(self,bbox,img):
self.time_since_update = 0
self.hits += 1
self.hit_streak += 1
'''re-start the tracker with detected positions (it detector was active)'''
if bbox != []:
self.tracker.start_track(img, rectangle(long(bbox[0]), long(bbox[1]), long(bbox[2]), long(bbox[3])))
'''
Note: another approach is to re-start the tracker only when the correlation score fall below some threshold
i.e.: if bbox !=[] and self.confidence < 10.
but this will reduce the algo. ability to track objects through longer periods of occlusions.
'''
def _css_to_rect(css):
"""
Convert a tuple in (top, right, bottom, left) order to a dlib `rect` object
:param css: plain tuple representation of the rect in (top, right, bottom, left) order
:return: a dlib `rect` object
"""
return dlib.rectangle(css[3], css[0], css[1], css[2])
def diaplay(self):
for track in self.tracks:
box = track.bbox
pt1 = (int(box[0]),int(box[1]))
pt2 = (int(box[2]),int(box[3]))
cv2.rectangle(self.oldimg,pt1,pt2,(255,255,255),3)
cv2.imshow(self.oldimg)
cv2.waitKey(1)
def display(self):
"""docstring for display"""
for track in self.tracks:
if track.DISPLAY:
bbox = track.box
pt1 = (bbox[0],bbox[1])
pt2 = (bbox[2],bbox[3])
cv2.rectangle(self.img, pt1, pt2, (255,255,255), 2)
# pass # we will display its bbox
cv2.imshow('Vedio', self.img)
cv2.waitKey(1)
print 'SHOW'
return
def display(self):
"""docstring for display"""
for track in self.tracks:
if track.DISPLAY:
bbox = track.box
pt1 = (bbox[0],bbox[1])
pt2 = (bbox[2],bbox[3])
cv2.rectangle(self.img, pt1, pt2, (255,255,255), 2)
# pass # we will display its bbox
cv2.imshow('Vedio', self.img)
cv2.waitKey(1)
print 'SHOW'
return
def display(self):
"""docstring for display"""
for track in self.tracks:
if track.DISPLAY:
bbox = track.box
pt1 = (bbox[0],bbox[1])
pt2 = (bbox[2],bbox[3])
cv2.rectangle(self.img, pt1, pt2, (255,255,255), 2)
# pass # we will display its bbox
cv2.imshow('Vedio', self.img)
cv2.waitKey(1)
print 'SHOW'
return
def showIMG(img, box, time=10):
cv2.rectangle(img, box[0:1], box[2:3],
(255,255,255), 2)
cv2.imshow('Image', img)
cv2.waitKey(time)
def start_tracking():
global updated
global i, img_count
global updtbox, oldbox, crtbox
while i <= img_count:
# get a new frame
img = cv2.imread(imdb_path+'/%04d.jpg'%i)
# update the tracker
if updated:
# tracker.start_track()
tracker.start_track(img,
dlib.rectangle(*updtbox))
oldbox = updtbox
updated = False
# post a new frame
trd_post = Thread(target=postIMG)
trd_post.start()
else:
# tracker.update()
tracker.update(img)
rect = tracker.get_position()
pt1 = [int(rect.left()), int(rect.top())]
pt2 = [int(rect.right()),int(rect.bottom())]
crtbox = pt1 + pt2
f.write(str(crtbox)+'\n')
if i%10 == 0:
print 'frame',i,'returns',crtbox
if showimg:
showIMG(img, crtbox, 2000)
# next frame
i +=1
def showIMG(img, box, time=10):
cv2.rectangle(img, box[0:1], box[2:3],
(255,255,255), 2)
cv2.imshow('Image', img)
cv2.waitKey(time)
def start_tracking():
global updated
global i, img_count
global updtbox, oldbox, crtbox
while i <= img_count:
# get a new frame
img = cv2.imread(imdb_path+'/%04d.jpg'%i)
# update the tracker
if updated:
# tracker.start_track()
tracker.start_track(img,
dlib.rectangle(*updtbox))
oldbox = updtbox
updated = False
# post a new frame
trd_post = Thread(target=postIMG)
trd_post.start()
else:
# tracker.update()
tracker.update(img)
rect = tracker.get_position()
pt1 = [int(rect.left()), int(rect.top())]
pt2 = [int(rect.right()),int(rect.bottom())]
crtbox = pt1 + pt2
f.write(str(crtbox)+'\n')
if i%10 == 0:
print 'frame',i,'returns',crtbox
if showimg:
showIMG(img, crtbox, 2000)
# next frame
i +=1
def init_firstframe_by_grdtruth():
global updtbox, oldbox, crtbox
gtfile = open(imdb_path+'/'+'groundtruth_rect.txt','r')
line = gtfile.readline()
points = line[:-1].split(',')
points = map(int, points)
points[2] += points[0]
points[3] += points[1]
gtfile.close()
crtbox = points
updtbox = crtbox
oldbox = crtbox
img = cv2.imread(imdb_path+'/0001.jpg')
tracker.start_track(img, dlib.rectangle(*crtbox))
def showIMG(img, box, time=10):
cv2.rectangle(img, box[0:1], box[2:3],
(255,255,255), 2)
cv2.imshow('Image', img)
cv2.waitKey(time)
def start_tracking():
global updated
global i, img_count
global updtbox, oldbox, crtbox
while i <= img_count:
# get a new frame
img = cv2.imread(imdb_path+'/%04d.jpg'%i)
# update the tracker
if updated:
# tracker.start_track()
tracker.start_track(img,
dlib.rectangle(*updtbox))
oldbox = updtbox
updated = False
# post a new frame
trd_post = Thread(target=postIMG)
trd_post.start()
else:
# tracker.update()
tracker.update(img)
rect = tracker.get_position()
pt1 = [int(rect.left()), int(rect.top())]
pt2 = [int(rect.right()),int(rect.bottom())]
crtbox = pt1 + pt2
f.write(str(crtbox)+'\n')
if i%10 == 0:
print 'frame',i,'returns',crtbox
if showimg:
showIMG(img, crtbox, 2000)
# next frame
i +=1
def CT_run(self, img, img_old, img_last):
""" CT_run:
-------
When called, it will // CT.update // the Correlation Tracker once.
if self.UPDATED, it will call // CT.start_track //.
"""
if self.UPDATED:
if self.UPDATE_ASSIGN:
self.CT.start_track(img_old, dlib.rectangle(*self.CT_box_update))
else:
self.CT.start_track(img_last, dlib.rectangle(*self.CT_box_update))
self._CT_turn_new_to_old() # turn new to old
self.CT.update(img)
# get current position and update // CT_box //
rect = self.CT.get_position()
self.CT_box = [int(rect.left()), int(rect.top()), \
int(rect.right()), int(rect.bottom())]
if self.UPDATED:
pass
# _CT_turn_new_to_old()
self.UPDATED = False
return
def display(self):
"""docstring for display"""
for track in self.tracks:
if track.DISPLAY:
bbox = track.box
pt1 = (bbox[0],bbox[1])
pt2 = (bbox[2],bbox[3])
cv2.rectangle(self.img, pt1, pt2, (255,255,255), 2)
# pass # we will display its bbox
cv2.imshow('Vedio', self.img)
cv2.waitKey(1)
print 'SHOW'
return
def _css_to_rect(css):
"""
Convert a tuple in (top, right, bottom, left) order to a dlib `rect` object
:param css: plain tuple representation of the rect in (top, right, bottom, left) order
:return: a dlib `rect` object
"""
return dlib.rectangle(css[3], css[0], css[1], css[2])
faceWarp.py 文件源码
项目:DelaunayVisualization-FacialWarp
作者: sneha-belkhale
项目源码
文件源码
阅读 20
收藏 0
点赞 0
评论 0
def drawColoredTriangles(img, triangleList, disp):
#sort the triangle list by distance from the top left corner in order to get a gradient effect when drawing triangles
triangleList=sorted(triangleList, cmp=triDistanceSort)
h, w, c = img.shape
#get bounding rectangle points of image
r = (0, 0, w, h)
#iterate through and draw all triangles in the list
for idx, t in enumerate(triangleList):
#grab individual vertex points
pt1 = [t[0], t[1]]
pt2 = [t[2], t[3]]
pt3 = [t[4], t[5]]
#select a position for displaying the enumerated triangle value
pos = (t[2], t[3])
#create the triangle
triangle = np.array([pt1, pt2, pt3], np.int32)
#select a color in HSV!! (manipulate idx for cool color gradients)
color = np.uint8([[[idx, 100, 200]]])
#color = np.uint8([[[0, 0, idx]]])
#convert color to BGR
bgr_color = cv2.cvtColor(color, cv2.COLOR_HSV2BGR)
color = (int(bgr_color[(0, 0, 0)]), int(bgr_color[(0, 0, 1)]), int(bgr_color[(0, 0, 2)]))
#draw the triangle if it is within the image bounds
if rect_contains(r, pt1) and rect_contains(r, pt2) and rect_contains(r, pt3):
cv2.fillPoly(img, [triangle], color)
# if display triangle number was selected, display the number.. this helps with triangle manipulation later
if(disp==1):
cv2.putText(img, str(idx), pos, fontFace=cv2.FONT_HERSHEY_SCRIPT_SIMPLEX, fontScale=0.3, color=(0, 0, 0))
######################################## example script ########################################
def to_dlib_rect(self):
import dlib
return dlib.rectangle(left=self.x1, right=self.x2, top=self.y1, bottom=self.y2)
def align(self, imgDim, rgbImg, bb=None,
landmarks=None, landmarkIndices=INNER_EYES_AND_BOTTOM_LIP,
skipMulti=False, scale=1.0):
r"""align(imgDim, rgbImg, bb=None, landmarks=None, landmarkIndices=INNER_EYES_AND_BOTTOM_LIP)
Transform and align a face in an image.
:param imgDim: The edge length in pixels of the square the image is resized to.
:type imgDim: int
:param rgbImg: RGB image to process. Shape: (height, width, 3)
:type rgbImg: numpy.ndarray
:param bb: Bounding box around the face to align. \
Defaults to the largest face.
:type bb: dlib.rectangle
:param landmarks: Detected landmark locations. \
Landmarks found on `bb` if not provided.
:type landmarks: list of (x,y) tuples
:param landmarkIndices: The indices to transform to.
:type landmarkIndices: list of ints
:param skipMulti: Skip image if more than one face detected.
:type skipMulti: bool
:param scale: Scale image before cropping to the size given by imgDim.
:type scale: float
:return: The aligned RGB image. Shape: (imgDim, imgDim, 3)
:rtype: numpy.ndarray
"""
assert imgDim is not None
assert rgbImg is not None
assert landmarkIndices is not None
if bb is None:
bb = self.getLargestFaceBoundingBox(rgbImg, skipMulti)
if bb is None:
return
if landmarks is None:
landmarks = self.findLandmarks(rgbImg, bb)
npLandmarks = np.float32(landmarks)
npLandmarkIndices = np.array(landmarkIndices)
#pylint: disable=maybe-no-member
H = cv2.getAffineTransform(npLandmarks[npLandmarkIndices],
imgDim * MINMAX_TEMPLATE[npLandmarkIndices]*scale + imgDim*(1-scale)/2)
thumbnail = cv2.warpAffine(rgbImg, H, (imgDim, imgDim))
return thumbnail