2

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.

antifriz
  • 873
  • 2
  • 13
  • 20

1 Answers1

1

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
a99
  • 301
  • 3
  • 5
  • its a python snippet which shows the extraction of images from a .vec file. – a99 Aug 21 '15 at 10:38
  • It is a python snippet which 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 demo for seeing how easy it is to extract an image form a .vec file. – a99 Aug 21 '15 at 10:49
  • Ok, thanks for the explanation. I am not the downvoter but I guess there will be even more downvotes as just posting code is bad style. Maybe you can incorporate your comments in the answer!? – Cleb Aug 21 '15 at 11:01