Skip to content

Instantly share code, notes, and snippets.

@ceremcem
Created February 14, 2016 01:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ceremcem/cb35728cc0fa2e39d25a to your computer and use it in GitHub Desktop.
Save ceremcem/cb35728cc0fa2e39d25a to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
__author__ = 'ceremcem'
import cv2
import numpy as np
def test_cv():
c = cv2.VideoCapture('1.jpg')
assert c.isOpened()
val, img = c.read()
assert val #this should be True
test_cv()
class Target:
def __init__(self):
self.frame1 = cv2.imread('1.jpg')
self.frame2 = cv2.imread('2.jpg')
cv2.namedWindow("Target", 1)
diff = self.frame2 - self.frame1
diff_gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
diff_gray = 255 - diff_gray
#diff_gray = self.blob_test
# Set up the detector with default parameters.
# Setup SimpleBlobDetector parameters.
params = cv2.SimpleBlobDetector_Params()
# Change thresholds
params.minThreshold = 1;
params.maxThreshold = 200;
# Filter by Area.
params.filterByArea = True
params.minArea = 15
# Filter by Circularity
params.filterByCircularity = True
params.minCircularity = 0.001
# Filter by Convexity
params.filterByConvexity = True
params.minConvexity = 0.87
# Filter by Inertia
params.filterByInertia = True
params.minInertiaRatio = 0.01
# Create a detector with the parameters
detector = cv2.SimpleBlobDetector(params)
# Detect blobs.
keypoints = detector.detect(diff_gray)
cv2.imwrite("output1.jpg", diff_gray)
#cv2.imwrite("output_with_keypoints", im_with_keypoints)
product_map = {
'kola': [{'x': [1580, 2128], 'y': [1908, 2740]}],
'kola zero': [{'x': [2164, 2340], 'y': [2336, 2540]}],
'fanta': [{'x': [2140, 2660], 'y': [2536, 2744]}],
}
taken_products = []
for i in keypoints:
x = i.pt[0]
y = i.pt[1]
#print "DEBUG: Moved blob: x: %f, y: %f" % (x, y)
for product, coords in product_map.iteritems():
for coor in coords:
#print "DEBUG: map of %s: " % product, coor['x'], coor['y']
try:
assert x >= coor['x'][0]
assert x <= coor['x'][1]
assert y >= coor['y'][0]
assert y <= coor['y'][1]
if product not in taken_products:
taken_products.append(product)
except:
pass
for p in taken_products:
print "taken product: ", p
if __name__=="__main__":
t = Target()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment