Demo entry 5905415

python code

   

Submitted by anonymous on Aug 31, 2016 at 10:08
Language: Python. Code size: 6.1 kB.

import numpy as np
import cv2
from multiprocessing import Process, Queue
from Queue import Full, Empty
from regions_proposal import validateGroundTruthStr, classifyWithGroundTruths
import os
import time
from random import shuffle

class Worker:
    def __init__(self):
        pass
    
    def work(self):
        pass
    
class ImageLoader(Worker):
    def __init__(self, image_path, img_name_list_file, rect_list_file=None):
        print "loading indexing file..."
        self.image_path = image_path
        self.image_list = []
        with open(img_name_list_file) as img_list_reader:
            for img_name in img_list_reader:
                self.image_list.append(img_name.strip())
        self.list_len = len(self.image_list)
        
        if rect_list_file is not None:
            self.rects_list = []
            with open(rect_list_file) as rect_list_reader:
                for rects_str in rect_list_reader:
                    rects = validateGroundTruthStr(rects_str)
                    self.rects_list.append(rects)
            assert self.list_len == len(self.rects_list)
            
        self.image_idx = 0
        
        print "done"
        
    def work(self, img_num):
        rt_img_list = []
        rt_rect_list = []
        for i in xrange(img_num):
            idx = self.image_idx % self.list_len
            rt_rect_list.append(self.rects_list[idx])
            img_name = self.image_list[idx]
            img_name = os.path.join(self.image_path, img_name)
            img = cv2.imread(img_name)
            assert img is not None
            rt_img_list.append(img)
            self.image_idx += 1
        
        return (rt_img_list, rt_rect_list)

class NegativeGenerator(Worker):
    def __init__(self):
        pass
    
    def set_list(self, img_list, rect_list):
        self.img_id = 0
        self.img_list = img_list
        self.rect_list = rect_list
        self.list_len = len(img_list)
        self.index = range(self.list_len)
        shuffle(self.index)
        
    def work(self, bch_sz, pch_sz):
        count_per_img = 5
        all_count = 0
        batch = np.zeros((pch_sz**2*3, bch_sz), np.uint8, order=order)
        i = self.img_id
        while all_count < bch_sz:           
            idx = self.index[i]
            img = self.img_list[idx]
            h, w = img.shape[:2]
            rects = self.rect_list[idx]
            cnt = 0
            
            while cnt < count_per_img:
                rsize = randint(min_size, min(w, h)-1)
                x = randint(0, w-1-rsize)
                y = randint(0, h-1-rsize)
                rect = (x, y, rsize, rsize)         
                    
                if rects.shape[0] == 0:
                    proposal_label, proposal_gt = Negative, None
                else:
                    proposal_label, proposal_gt = classifyWithGroundTruths(rect, rects)
                
                if proposal_label == Negative:
                    patch = get_image(img, rect)
                    if patch is None:
                        continue
                    patch = cv2.resize(patch, (pch_sz, pch_sz))
                    
                    if all_count < bch_sz:
                        batch[:, all_count] = patch.swapaxes(0,1).swapaxes(0,2).flatten(order)    
                        cnt += 1
                        all_count += 1
                    else:
                        break
                    
            i = (i + 1) % self.list_len
            if i == 0:                    
                shuffle(self.index)
                self.img_id = 0
        else:
            self.img_id = i

        #print "img_id", self.img_id
        #print "list_len", self.list_len
        return batch    
    
class PositiveGenerator(Worker):
    def __init__(self):
        pass
    
    def set_list(self, img_list, rect_list):
        self.img_id = 0
        self.img_list = img_list
        self.rect_list = rect_list
        self.list_len = len(img_list)
        self.index = range(self.list_len)
        shuffle(self.index)
        
    def work(self, bch_sz, pch_sz):
        pass

def load_image_process(data_queue, img_loader, img_num):
    buff_queue = Queue(1)
    data = None
    while 1:
        try:
            if not buff_queue.full():
                print "before put", buff_queue.qsize()
                data = img_loader.work(img_num)
                #print len(data[0]), len(data[1])
                buff_queue.put(data)
                print "after put", buff_queue.qsize()                
                print "Producer produced one block of data"
            if not data_queue.full():
                if not buff_queue.empty():
                    data = buff_queue.get()
                    data_queue.put(data, block=False)
            else:
                print "Inside, Queue is full, task a rest"
                time.sleep(1)
        except KeyboardInterrupt:      
            print "Keyboard Interrupt"
            return        
        except Full:
            print "Queue is full, task a rest"
            time.sleep(1)
        except:
            print "Producer Unexcepted error"                
    
        
def sample_generator_process(img_queue, sample_queue, )        
    pass



if __name__ == '__main__':
    image_path = "D:/GK/WorkSpace/Code/Python/tools/FaceData/images/"
    data_path = "D:/GK/WorkSpace/Code/Python/tools/FaceData/data/CelebA/"
    img_name_list_file = "images_ground_truth.txt"
    rect_list_file = "rects_ground_truth.txt"
    img_loader = ImageLoader(image_path, 
                             data_path + img_name_list_file,
                             data_path + rect_list_file
                            )
    
    pos_generators = []
    num_pos = 2
    for i in range(num_pos):
        pos_generators.append(PositiveGenerator())
    
    image_queue = Queue(2)    
    load_image_process(image_queue, img_loader, 10)

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).