Script opencv_createsamples creates .vec file that holds all images necessary for training cascades.
I am wondering if there is a way to load images directly from that file to python script.
Thank you.
Script opencv_createsamples creates .vec file that holds all images necessary for training cascades.
I am wondering if there is a way to load images directly from that file to python script.
Thank you.
This python snippet shows the extraction of images from a .vec file. The function can be called with showvec('xxx.vec'). The width and height parameters must only be set, if the images in the vec file has other dimensions as standard. ( 24 x 24 ) The resize parameter resizes the image for better seeing.When calling this function a window will show the images one after the other when hitting a key. The esc key will break the loop.Its a demonstration how easy it is to extract an image form a .vec file with python.
import struct,array
import cv2
import numpy as np
def showvec(fn, width=24, height=24, resize=4.0):
f = open(fn,'rb')
HEADERTYP = '<iihh' # img count, img size, min, max
# read header
imgcount,imgsize,_,_ = struct.unpack(HEADERTYP, f.read(12))
for i in range(imgcount):
img = np.zeros((height,width),np.uint8)
f.read(1) # read gap byte
data = array.array('h')
### buf = f.read(imgsize*2)
### data.fromstring(buf)
data.fromfile(f,imgsize)
for r in range(height):
for c in range(width):
img[r,c] = data[r * width + c]
img = cv2.resize(img, (0,0), fx=resize, fy=resize, interpolation=cv2.INTER_LINEAR)
cv2.imshow('vec_img',img)
k = 0xFF & cv2.waitKey(0)
if k == 27: # esc to exit
break