Skip to content

Instantly share code, notes, and snippets.

@Lukse
Created August 21, 2016 10:21
Show Gist options
  • Save Lukse/29cafd7d980b94f8e791bc0a7a6ffb5a to your computer and use it in GitHub Desktop.
Save Lukse/29cafd7d980b94f8e791bc0a7a6ffb5a to your computer and use it in GitHub Desktop.
Detect barcodes
# -*- coding: utf-8 -*-
from sys import argv
import zbar
from PIL import Image
import cv2
import numpy as np
from tqdm import trange
def detect(im):
pil = Image.fromarray(im).convert('L')
scanner = zbar.ImageScanner()
scanner.parse_config('enable')
width, height = pil.size
raw = pil.tobytes()
zimage = zbar.Image(width, height, 'Y800', raw)
scanner.scan(zimage)
return zimage
def append_unique(l, val):
try:
if val not in l:
l.append(val)
except:
pass
return l
numbers = []
cap = cv2.VideoCapture("input.avi")
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
for i in trange(frame_count, unit=' frames', leave=False, dynamic_ncols=True, desc='Calculating blur ratio'):
ret, im = cap.read()
im = cv2.GaussianBlur(im, (3, 3), 20)
zimage = detect(im)
i = 0
for symbol in zimage:
i += 1
for l in symbol.location:
text = str(symbol.type) + ': ' + str(symbol.data) + ' / ' + str(symbol.quality)
cv2.circle(im, (int(l[0]), int(l[1])), 4, (255, 255, 255), -1)
cv2.putText(im, text, (20, 20 + i*30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
contour = np.array(symbol.location, dtype=np.int)
x, y, w, h = cv2.boundingRect(contour)
cv2.rectangle(im, (x, y), (x+w, y+h), (0, 0, 255), 2)
numbers = append_unique(numbers, symbol.data)
scale = 0.5
im = cv2.resize(im, None, fx=scale, fy=scale, interpolation = cv2.INTER_CUBIC)
cv2.imshow("im", im)
k = cv2.waitKey(1) & 0xff
if k == 27:
break
print numbers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment