fetch sketches

import numpy as np
import cv2
import time
import os
import random
import threading
import multiprocessing
import urllib
import warnings
from multiprocessing import Lock
warnings.filterwarnings("ignore")
def convert2Sketch( img, white_threshold ):
    scope = 255 / (255-white_threshold)
    b = -scope * white_threshold
    [m,n,c] = img.shape
    # Remove colors
    if c > 1:
        temp_img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#         img_gray[:,:,1] = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY);   
#         img_gray[:,:,2] = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY);   
#         img_gray[:,:,3] = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY);
    else:
        temp_img_gray = img
#         img_gray[:,:,1] = img;   
#         img_gray[:,:,2] = img;   
#         img_gray[:,:,3] = img;
    img_gray = [temp_img_gray,temp_img_gray,temp_img_gray]
    img_gray = np.array(img_gray).swapaxes(0,1)
    img_gray = np.array(img_gray).swapaxes(1,2)
#     print(np.array(img_gray).shape)
#     figure('Name', 'Remove colors');  imshow(img_gray); 
#     cv2.imwrite("test1.jpg",img_gray)
    # Adjust curve
    for i in range(m):
        for j in range(n):
#             print(img_gray[i, j, 1])
            if img_gray[i, j, 0] <= white_threshold:
                img_gray[i, j, :] = np.zeros(3);
            else:
                img_gray[i, j, :] = scope * img_gray[i, j, :] + b;

#     figure('Name','Adjust curve'); imshow(uint8(img_gray));
    
    # Copy layer 1 to layer 2 and reverse colors.
    img_gray2 = np.zeros([m, n, 3]);
    img_mix = np.zeros([m, n, 3]);
    for i in range(m):
        for j in range(n):
            img_gray2[i, j, 0] = 255 - img_gray[i, j, 0];
            img_gray2[i, j, 1] = 255 - img_gray[i, j, 1];
            img_gray2[i, j, 2] = 255 - img_gray[i, j, 2];

    
    # Min filter
    radius = 1;
#     cv2.imwrite("test2.jpg",img_gray2)
    img_gray2 = min_filter(img_gray2, radius);
#     cv2.imwrite("test3.jpg",img_gray2)
#     figure('Name', 'Min filter'); imshow(uint8(img_gray2));
    
    # Mix layers
    img_mix = color_dodge(img_gray2, img_gray);
#     cv2.imwrite("test4.jpg",img_mix)
    img_mix = np.array(img_mix).astype(np.uint8)
#     cv2.imwrite("test5.jpg",img_mix)
    res = np.array(img_mix).astype(np.uint8);
    return res
#     figure('Name', 'Mix layers'); imshow(res); 

# Function for layer mixture
def  color_dodge(layer1, layer2):
    #((uint8)((B == 255) ? B:min(255, ((A << 8 ) / (255 - B)))))
    [m, n, c] = layer2.shape;
    if c == 1:
        res = np.zeros([m, n]);
        for i in range(m):
            for j in range(n):
                if layer2[i, j] == 255:
                    res[i, j] = 255;
                else:
                    res[i, j] = min(255, (layer1[i, j]*256 / (255 - layer2[i, j])));
    else:
        res = np.zeros([m, n, c]);
        for i in range(m):
            for j in range(n):
                for k in range(c):
                    if layer2[i, j, k] == 255:
                        res[i, j, k] = 255;
                    else:
                        res[i, j, k] = min(255, (layer1[i, j, k]*256 / (255 - layer2[i, j, k])));
    return res

# Function for min filter
def  min_filter(img, radius):
    [m, n, c] = img.shape;
    filter_width = 1 + 2 * radius;
    if c == 1:
        res = np.zeros([m, n]);
        for i in range( m-2*radius ):
            for j in range(n-2*radius ):
                current_min = np.min(np.min(img[i:i+2*radius, j:j+2*radius]));
                res[i:i+2*radius, j:j+2*radius] = np.ones([filter_width, filter_width]) * float(current_min); 
    else:
        res = np.zeros([m, n, c]);
        for i in range(m-2*radius):
            for j in range(n-2*radius):
                for k in range(c):
                    current_min = np.min(np.min(img[i:i+2*radius, j:j+2*radius, k]));
                    res[i:i+2*radius+1, j:j+2*radius+1, k] = np.ones([filter_width, filter_width]) * float(current_min);
    return res
# Function for max filter
def max_filter(img, radius):
    [m, n, c] = img.shape;
    filter_width = 1 + 2 * radius;
    if c == 1:
        res = np.zeros([m, n]);
        for i in range(m-2*radius):
            for j in range(n-2*radius):
                current_max = np.max(np.max(img[i:i+2*radius+1, j:j+2*radius+1]));
                res[i:i+2*radius+1, j:j+2*radius+1]= np.ones([filter_width, filter_width]) * float(current_max);
    else:
        res = np.zeros([m, n, c]);
        for i in range(m-2*radius):
            for j in range(n-2*radius):
                for k in range(c):
                    current_max = np.max(np.max(img[i:i+2*radius+1, j:j+2*radius+1, k]));
                    res[i:i+2*radius+1, j:j+2*radius+1, k] = np.ones([filter_width, filter_width]) * float(current_max);
    return res







def get_image_url(index, index2, index3, line, input_filename,output_filename ):
    try:
        
        try:
            lock = Lock()
            lock.acquire()
            img = cv2.imread(input_filename)
            img = np.array(np.array(img).astype(np.float64) * 1.5).astype(np.uint8);
            white_threshold = 40;
            img_res = convert2Sketch(img, white_threshold);
            cv2.imwrite(output_filename,img_res)
        except Exception as e:
            print(e, index, index2, index3, line)
        finally:
            lock.release()
        
    except Exception as e:
        pass
    finally:
        pass
        
def running_processing(index, index2, lines, input_filename,output_filename):
    threads = []
    for index3, line in enumerate(lines) :
        print("--------------------------line------------------------------",index, index2, index3, line)
        t = threading.Thread(target= get_image_url, args=(index, index2, index3, line, input_filename[index3], output_filename[index3]))
        threads.append(t)
    for index_i, thread in enumerate(threads):
#         thread.setDaemon(True)
        thread.start()
    for index_j, thread in enumerate(threads):
        thread.join()
    
if __name__ == "__main__":

    Threads_number = 10
    Processes_number = 4

    input_path = "input"
    output_path = "output"
    if not os.path.exists(output_path):
        os.makedirs(output_path)

    index = 0

    temp_lines = []
    file_name_list = []
    output_file_name_list = []

    all_temp_lines = []
    all_file_name_list = []
    all_output_file_name_list = []

    all_lines = os.listdir(input_path)
    len_all_lines = len(all_lines)
    for index,line in enumerate(all_lines):
        try:
            file_name = input_path + "/" + line
            output_file_name = output_path + "/" + line
            
            temp_lines.append(line)
            file_name_list.append(file_name)
            output_file_name_list.append(output_file_name)

            if len(temp_lines) % Threads_number == 0 or len_all_lines == (index + 1):
                all_temp_lines.append(temp_lines)
                all_file_name_list.append(file_name_list)
                all_output_file_name_list.append(output_file_name_list)

                if len(all_temp_lines) % Processes_number == 0 or len_all_lines == (index + 1):
                    # print("len(temp_lines)", len(temp_lines))
                    # print("len(all_temp_lines)", len(all_temp_lines))
                    multiThreads = []

                    for index2, value in enumerate(all_temp_lines):
                        mt = multiprocessing.Process(target=running_processing,args=(index, index2, all_temp_lines[index2], all_file_name_list[index2], all_output_file_name_list[index2]))
                        mt.start()
                        multiThreads.append(mt)
                    
                    for mthread in multiThreads:
                        mthread.join()

                    all_temp_lines = []
                    all_file_name_list = []
                    all_output_file_name_list = []

                temp_lines = []
                file_name_list = []
                output_file_name_list = []
        except Exception as e:
            print(e,line)

你可能感兴趣的:(fetch sketches)