def get_pre_output(Gs, initial_noise, images, fix=True): generator_num = len(Gs) pre_output = initial_noise if fix == True: for i in range(generator_num): shape = images[i].shape generator = Gs[i] if i == 0: output = generator(pre_output, training=False) pre_output = output else: upsampling_out = utils.image_resize(pre_output, shape[1], shape[2]) output = generator(upsampling_out, training=False) pre_output = output + upsampling_out return pre_output if fix == False: for i in range(generator_num): shape = images[i].shape generator = Gs[i] if i == 0: output = generator(pre_output, training=False) pre_output = output else: noise = tf.random.uniform(shape) upsampling_out = utils.image_resize(pre_output, shape[1], shape[2]) output = generator(upsampling_out + 0.1 * noise, training=False) pre_output = output + upsampling_out return pre_output
def __getitem__(self, idx): img_fn = self.fns[idx] image = np.array(Image.open(img_fn).convert('RGB')) # resize w.r.t. short size or long size if self.short_size is not None or self.long_size is not None: image = utils.image_resize(image, short_size=self.short_size, long_size=self.long_size) # crop centerx = image.shape[1] // 2 centery = image.shape[0] // 2 sizex, sizey = self.crop_size bbox = [centerx - sizex / 2, centery - sizey / 2, sizex, sizey] image = cv2.resize(utils.crop_padding(image, bbox, pad_value=(0, 0, 0)), self.crop_size, interpolation=cv2.INTER_CUBIC) # transform image = torch.from_numpy(image.astype(np.float32)).transpose( (2, 0, 1)) # 3HW image = self.transforms(image) return image
def split(content, filename, mode): M = content.shape[0] // 4 N = content.shape[1] // 4 splited = [content[x:x+M,y:y+N] for x in range(0,content.shape[0],M) for y in range(0,content.shape[1],N)] for idx, img in enumerate(splited): img = image_resize(img, size=(512, 512)) fileID = filename.split('/')[-1].split('_')[0] postfix = 'sat.jpg' if mode == 'sat' else 'mask.png' output_name = '{}-{:02d}_{}'.format(fileID, idx, postfix) print(output_name) skimage.io.imsave(os.path.join(args.output_path, output_name), img)
def __init__(self, content_image, style_image, width, height, channel, content_w, style_w, training_steps, logging_steps=1): self.img_width = width self.img_height = height self.img_channel = channel self.input_image = None self.content = content_image.split('/')[-1][:-4] self.style = style_image.split('/')[-1][:-4] self.content_img = utils.image_resize(content_image, self.img_width, self.img_height, save=False) self.style_img = utils.image_resize(style_image, self.img_width, self.img_height, save=False) self.initial_img = utils.generate_noise_image(self.content_img, self.img_width, self.img_height) self.content_layer = ['conv4_2'] self.style_layers = ['conv1_1', 'conv2_1', 'conv3_1', 'conv4_1', 'conv5_1'] self.content_w = content_w self.style_w = style_w self.content_layer_w = [1.0] self.style_layer_w = [0.5, 1.0, 1.5, 3.0, 4.0] # [0.2] * 5 self.vgg19 = None # VGG19 model self.content_loss = 0. self.style_loss = 0. self.total_loss = 0. # Hyper-Parameters self.train_steps = training_steps self.logging_steps = logging_steps self.g_step = tf.Variable(0, trainable=False, name='global_steps') self.opt = None self.summary = None self.lr = 1. self.build()
def _images_rgb(images_path): images_list = os.listdir(images_path) images_rgb = [] count = 0 for image in images_list: print('rgb in {0}.'.format(count)) image_path = os.path.join(images_path, image) image_data = cv2.imread(image_path) image_resized = image_resize(image_data, IMAGE_SIZE) image_rgb = convert_bgr_to_rgb(image_resized) images_rgb.append(image_rgb) count += 1 return images_list, images_rgb
def get_frame(): if args.image is None: new_frame = vs.read() if isinstance(new_frame, tuple): new_frame = new_frame[1] else: new_frame = cv2.imread(args.image).astype(np.float32) if new_frame is None: print("frame is None. Possibly camera or display does not work") return None if new_frame.shape[0] > 480: new_frame = utils.image_resize(new_frame, height=480) return new_frame
def mobile(): try: image = request.files['image'] except: return jsonify({'result': 'failure'}) image.save(os.path.join('static/uploaded', image.filename)) img = Image.open(os.path.join('static/uploaded', image.filename)) img = image_resize(img) img.save(os.path.join('static/uploaded', image.filename)) img = tf.keras.preprocessing.image.img_to_array(img) img = img / 255 calories = model.predict( tf.data.Dataset.from_tensor_slices([img]).batch(1)) to_return = {'result': 'success', 'calories': int(calories[0][0])} return jsonify(to_return)
def __getitem__(self, idx): img_fn = self.metas[idx] img = pil_loader_str(img_fn, ch=3) ## resize if self.short_size is not None or self.long_size is not None: img, size = image_resize(img, short_size=self.short_size, long_size=self.long_size) ## crop if self.crop_size is not None: img, offset = image_crop(img, self.crop_size) ## transform img = torch.from_numpy(np.array(img).astype(np.float32).transpose((2,0,1))) img = self.img_transform(img) return img, torch.LongTensor([idx]), torch.LongTensor(offset), torch.LongTensor(size)
def eval(self): # print('loading model from {}...'.format(self.model.model_path)) # self.model.load(self.model.model_path) # print('Model is loaded!!!') self.model.build_model() content_path, _ = get_file_paths(self.test_path) content_path.sort() for path in content_path: fileID = path.split('/')[-1].split('_')[0] output_name = '{}_mask.png'.format(fileID) output_name = os.path.join(self.output_path, output_name) print(output_name) img = np.expand_dims(vgg_sub_mean(get_image(path)), axis=0) reconst_mask = self.model.decode(img) reconst_mask = mask_postprocess(reconst_mask[0]) reconst_mask = image_resize(reconst_mask, size=(612, 612)) skimage.io.imsave(output_name, reconst_mask)
def get_single_words(self, word_size=32): bg = np.zeros((word_size, word_size, 1), dtype=np.uint8) bg += 255 ratio = 0.9 self.all_words = [] i = 0 for rect in self.all_rects: word = self.crop_img[rect[1]:rect[3], rect[0]:rect[2]] # self.crop_img[rect[1]:rect[3], rect[0]:rect[2]] = 0 small_word = utils.image_resize(word, 32 * ratio) canvas = bg.copy() w = small_word.shape[1] h = small_word.shape[0] x = int((word_size - small_word.shape[1]) / 2) y = int((word_size - small_word.shape[0]) / 2) canvas[y:y + h, x:x + w] = np.expand_dims(small_word, axis=2) finale = cv2.cvtColor(canvas, cv2.COLOR_GRAY2RGB) self.all_words.append(finale)
def home(): if request.method == 'GET': return render_template('page.html') else: image = request.files['image'] image.save(os.path.join('static/uploaded', image.filename)) img = Image.open(os.path.join('static/uploaded', image.filename)) img = image_resize(img) img.save(os.path.join('static/uploaded', image.filename)) img = tf.keras.preprocessing.image.img_to_array(img) img = img / 255 calories = model.predict( tf.data.Dataset.from_tensor_slices([img]).batch(1)) return render_template( 'page.html', calories=str(int(calories[0][0])) + " Килокалорий", source=url_for('static', filename='uploaded/' + image.filename))
def __getitem__(self, idx): self._init_memcached() img = self._read_one(idx) ## resize if self.short_size is not None or self.long_size is not None: img, size = image_resize(img, short_size=self.short_size, long_size=self.long_size) ## crop if self.crop_size is not None: img, offset = image_crop(img, self.crop_size) ## transform img = torch.from_numpy( np.array(img).astype(np.float32).transpose((2, 0, 1))) img = self.img_transform(img) return img, torch.LongTensor( [idx]), torch.LongTensor(offset), torch.LongTensor(size)
import numpy as np import cv2 from utils import CFEVideoConf, image_resize cap = cv2.VideoCapture(0) save_path = 'saved-media/watermark.mp4' frames_per_seconds = 24 config = CFEVideoConf(cap, filepath=save_path, res='720p') out = cv2.VideoWriter(save_path, config.video_type, frames_per_seconds, config.dims) img_path = 'images/logo/cfe-coffee.png' logo = cv2.imread(img_path, -1) watermark = image_resize(logo, height=50) #watermark = cv2.cvtColor(watermark, cv2.COLOR_BGR2GRAY) #watermark = cv2.cvtColor(watermark, cv2.COLOR_GRAY2BGR) watermark = cv2.cvtColor(watermark, cv2.COLOR_BGR2BGRA) # grayscale watermark # cv2.imshow('watermark', watermark) #print(watermark.shape) while (True): # Capture frame-by-frame ret, frame = cap.read() frame = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA) # print(frame[50, 150]) # numpy array # start_cord_x = 50 # start_cord_y = 150
import face_recognition import numpy as np import cv2 import random from PIL import Image, ImageDraw from utils import image_resize print("Engine revving...") print("Deep Learning activated") print("Hit 'q' to quit, 'c' to see") cap = cv2.VideoCapture(0) logo = cv2.imread('images/meme.png', -1) watermark = image_resize(logo, height=200) watermark = cv2.cvtColor(watermark, cv2.COLOR_BGR2BGRA) face_cascade = cv2.CascadeClassifier('xml/haarcascade_frontalface_default.xml') eyes_cascade = cv2.CascadeClassifier('xml/frontalEyes35x16.xml') nose_cascade = cv2.CascadeClassifier('xml/Nose18x15.xml') glasses = cv2.imread("images/glasses.png", -1) mustache = cv2.imread('images/mustache.png', -1) font = cv2.FONT_HERSHEY_SIMPLEX def apply_invert(frame): return cv2.bitwise_not(frame) rand_num = random.random()
def get_video_type(filename): filename, ext = os.path.splitext(filename) if ext in VIDEO_TYPE: return VIDEO_TYPE[ext] return VIDEO_TYPE['avi'] cap = cv2.VideoCapture(0) #video_writer = cv2.VideoWriter_fourcc(*'MJPG') #out = cv2.VideoWriter(filename,video_writer,12, get_dims(cap, res)) out = cv2.VideoWriter(filename, get_video_type(filename), frames_per_second, get_dims(cap, res)) img_path = 'images/logos/flag.png' logo = cv2.imread(img_path, -1) watermark = image_resize(logo, height=150, width=150) watermark = cv2.cvtColor(watermark, cv2.COLOR_BGR2BGRA) #cv2.imshow('watermark', watermark) print(watermark.shape) while True: ret, frame = cap.read() frame = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA) #OVERLAY WITH 4 CHANNELS & ALPHA frame_h, frame_w, frame_c = frame.shape #print(frame.shape) #gray = cv2.cvtColor(frame.copy(), cv2.COLOR_BGR2GRAY) #print(gray.shape) overlay = np.zeros((frame_h, frame_w, 4), dtype='uint8') #overlay[100:250,100:125] = (255,255,0,1) #overlay[100:250,150:255] = (0,255,0,1)
def run(): cap = cv2.VideoCapture(0) save_path = 'saved-media/glasses_and_stash.mp4' frames_per_seconds = 24 config = CFEVideoConf(cap, filepath=save_path, res='720p') out = cv2.VideoWriter(save_path, config.video_type, frames_per_seconds, config.dims) face_cascade = cv2.CascadeClassifier( 'cascades/data/haarcascade_frontalface_default.xml') eyes_cascade = cv2.CascadeClassifier( 'cascades/third-party/frontalEyes35x16.xml') nose_cascade = cv2.CascadeClassifier('cascades/third-party/Nose18x15.xml') glasses = cv2.imread("images/glasses/11.png", -1) mustache = cv2.imread("images/mustache/mustache.png", -1) imgHair = cv2.imread('images/hair/2.png', -1) while (True): # Capture frame-by-frame ret, frame = cap.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, scaleFactor=1.5, minNeighbors=5) frame = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA) for (x, y, w, h) in faces: y = y - 70 roi_gray = gray[y:y + h, x:x + h] # rec roi_color = frame[y:y + h, x:x + h] #cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 255, 255), 3) eyes = eyes_cascade.detectMultiScale(roi_gray, scaleFactor=1.5, minNeighbors=5) for (ex, ey, ew, eh) in eyes: #cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 3) roi_eyes = roi_gray[ey:ey + eh, ex:ex + ew] glasses2 = image_resize(glasses.copy(), width=ew) gw, gh, gc = glasses2.shape for i in range(0, gw): for j in range(0, gh): #print(glasses[i, j]) #RGBA if glasses2[i, j][3] != 0: # alpha 0 roi_color[ey + i, ex + j] = glasses2[i, j] nose = nose_cascade.detectMultiScale(roi_gray, scaleFactor=1.5, minNeighbors=5) for (nx, ny, nw, nh) in nose: #cv2.rectangle(roi_color, (nx, ny), (nx + nw, ny + nh), (255, 0, 0), 3) roi_nose = roi_gray[ny:ny + nh, nx:nx + nw] mustache2 = image_resize(mustache.copy(), width=nw) mw, mh, mc = mustache2.shape for i in range(0, mw): for j in range(0, mh): #print(glasses[i, j]) #RGBA if mustache2[i, j][3] != 0: # alpha 0 roi_color[ny + int(nh / 3.0) + i, nx + j] = mustache2[i, j] face = face_cascade.detectMultiScale(roi_gray, scaleFactor=1.5, minNeighbors=5) for (fx, fy, fw, fh) in face: fx = fx - 40 fy = fy - 120 #cv2.rectangle(roi_color, (fx, fy), (fx + fw, fy + fh), (255, 0, 0), 3) roi_face = roi_gray[fy:fy + fh, fx:fx + fw] imgHair2 = image_resize(imgHair.copy(), width=fw + int(fw / 6)) hw, hh, hc = imgHair2.shape for i in range(0, hw): for j in range(0, hh): #print(glasses[i, j]) #RGBA if imgHair2[i, j][3] != 0: # alpha 0 roi_color[fy + i, fx + j] = imgHair2[i, j] # Display the resulting frame frame = cv2.cvtColor(frame, cv2.COLOR_BGRA2BGR) out.write(frame) cv2.imshow('frame', frame) if cv2.waitKey(20) & 0xFF == ord('q'): break # When everything done, release the capture cap.release() out.release() cv2.destroyAllWindows()
minNeighbors=5) frame = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA) for (x, y, w, h) in faces: roi_gray = gray[y:y + h, x:x + h] # rec roi_color = frame[y:y + h, x:x + h] #cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 255, 255), 3) eyes = eyes_cascade.detectMultiScale(roi_gray, scaleFactor=1.5, minNeighbors=5) for (ex, ey, ew, eh) in eyes: #cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 3) roi_eyes = roi_gray[ey:ey + eh, ex:ex + ew] glasses2 = image_resize(glasses.copy(), width=ew) gw, gh, gc = glasses2.shape for i in range(0, gw): for j in range(0, gh): #print(glasses[i, j]) #RGBA if glasses2[i, j][3] != 0: # alpha 0 roi_color[ey + i, ex + j] = glasses2[i, j] nose = nose_cascade.detectMultiScale(roi_gray, scaleFactor=1.5, minNeighbors=5) for (nx, ny, nw, nh) in nose: #cv2.rectangle(roi_color, (nx, ny), (nx + nw, ny + nh), (255, 0, 0), 3) roi_nose = roi_gray[ny:ny + nh, nx:nx + nw] mustache2 = image_resize(mustache.copy(), width=nw)
import numpy as np import cv2 from utils import image_resize cap = cv2.VideoCapture(0) img_path = 'merve.jpg' logo = cv2.imread(img_path, -1) deneme = image_resize(logo, height= 100,width=100 ) deneme = cv2.cvtColor(deneme, cv2.COLOR_BGR2BGRA) while 1: # Capture frame-by-frame ret, frame = cap.read() frame = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA) frame_h, frame_w, frame_c = frame.shape # overlay with 4 channels BGR and Alpha overlay = np.zeros((frame_h, frame_w, 4), dtype='uint8') deneme_h, deneme_w, deneme_c = deneme.shape # replace overlay pixels with watermark pixel values for i in range(0, deneme_h): for j in range(0, deneme_w): # if watermark[i,j][3] != 0: offset = 10 h_offset = frame_h - deneme_h - offset w_offset = frame_w - deneme_w - offset overlay[i , offset +j] = deneme[i,j]
def eval(self, output_mode): # print('loading model from {}...'.format(self.model.model_path)) # self.model.load(self.model.model_path) # print('Model is loaded!!!') def merge(group, size=(612, 612), num_per_side=13): np_mask = np.zeros((2448, 2448, 7)) # img = Image.new('RGB', (2448, 2448)) for idx, chunk in enumerate(group): offset_x = idx // num_per_side * size[0] // 4 offset_y = idx % num_per_side * size[1] // 4 np_mask[offset_x:offset_x + size[0], offset_y:offset_y + size[1], :] = np_mask[offset_x:offset_x + size[0], offset_y:offset_y + size[1], :] + chunk[:, :, :] img = mask_postprocess(np_mask) img = Image.fromarray(img) return img self.model.build_model() content_path, _ = get_file_paths(self.test_path) content_path.sort() group = [] for cnt, path in enumerate(content_path): # assert len(content_path) % 169 == 0 img = np.expand_dims(vgg_sub_mean(get_image(path)), axis=0) reconst_mask = self.model.decode(img) if output_mode == 'img': fileID = path.split('/')[-1].split('_')[0] output_name = '{}_mask.png'.format(fileID) output_name = os.path.join(self.output_path, output_name) print("({}/{}) {}".format(cnt, len(content_path), output_name)) reconst_mask = mask_postprocess(reconst_mask[0]) reconst_mask = image_resize(reconst_mask, size=(612, 612)) skimage.io.imsave(output_name, reconst_mask) elif output_mode == 'npz': if cnt % 169 == 0 and cnt != 0: print('Saving {}'.format(output_name)) img = merge(group) img.save(output_name) group = [] fileID = path.split('/')[-1].split('-')[0] output_name = '{}_mask.png'.format(fileID) output_name = os.path.join(self.output_path, output_name) print("({}/{}) {}".format(cnt + 1, len(content_path), output_name)) scale = 612. / 512. reconst_mask = scipy.ndimage.interpolation.zoom( reconst_mask[0], zoom=(scale, scale, 1), mode='reflect') group.append(reconst_mask) # Saving the last figure if output_mode == 'npz': print('Saving {}'.format(output_name)) img = merge(group) img.save(output_name)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA) for (x, y, w, h) in faces if faces != () else faces_prev: roi_gray = gray[y:y + w, x:x + h] # rec roi_color = frame[y:y + w, x:x + h] roi_color_hat = frame[max(0, y - 200):y + h, x:x + w] eyes = eyes_cascade.detectMultiScale(roi_gray, scaleFactor=1.5, minNeighbors=5) faces_prev = faces if faces != () else faces_prev try: hat2 = image_resize(hat.copy(), width=w) hw, hh, hc = hat2.shape for i in range(0, hw): for j in range(0, hh): if hat2[i, j][3] != 0: roi_color_hat[i, j] = hat2[i, j] except IndexError: pass try: for (ex, ey, ew, eh) in eyes if eyes != () else eyes_prev: #cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 3) glasses2 = image_resize(glasses.copy(), width=eh) eyes_prev = eyes if eyes != () else eyes_prev
import numpy as np import cv2 from utils import CFEVideoConf, image_resize filename = 'watermark.mp4' frames_per_seconds = 24.0 cap = cv2.VideoCapture(0) config = CFEVideoConf(cap, filepath=filename, res='720p') out = cv2.VideoWriter(filename, config.video_type, frames_per_seconds, config.dims) watermark = 'Marketing-Communications-Logo-1.png' image = cv2.imread(watermark, -1) image = image_resize(image, 100, 100) cv2.imshow('watermark', image) while True: ret, frame = cap.read() out.write(frame) cv2.imshow('frame', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() out.release() cv2.destroyAllWindows()
scaleFactor=1.5, minNeighbors=5) frame = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA) for (x, y, w, h) in faces: roi_gray = gray[y:y + h, x:x + h] # rec roi_color = frame[y:y + h, x:x + h] nose = nose_cascade.detectMultiScale(roi_gray, scaleFactor=1.5, minNeighbors=5) for (nx, ny, nw, nh) in nose: roi_nose = roi_gray[ny:ny + nh, nx:nx + nw] mustache2 = image_resize(mustache.copy(), width=nw) mw, mh, mc = mustache2.shape for i in range(0, mw): for j in range(0, mh): if mustache2[i, j][3] != 0: # alpha 0 roi_color[ny + int(nh / 2.0) + i, nx + j] = mustache2[i, j] # Display the resulting frame frame = cv2.cvtColor(frame, cv2.COLOR_BGRA2BGR) cv2.imshow('frame', frame) if cv2.waitKey(20) & 0xFF == ord('x'): break
roi_gray = gray[y:y+h, x:x+h] # rec roi_color = frame[y:y+h, x:x+h] cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 255, 255), 3) eyes = eyes_cascade.detectMultiScale( roi_gray, scaleFactor=1.5, minNeighbors=5) for (ex, ey, ew, eh) in eyes: cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 3) roi_eyes = roi_gray[ey: ey + eh, ex: ex + ew] centers.append((x + int(ex + 0.5*ew), y + int(ey + 0.5*eh))) if len(centers) > 1: # Overlay sunglasses sunglasses_width = 1.9 * abs(centers[1][0] - centers[0][0]) glasses2 = image_resize( glasses.copy(), width=int(sunglasses_width)) gw, gh, gc = glasses2.shape for i in range(0, gw): for j in range(0, gh): # print(glasses[i, j]) #RGBA if glasses2[i, j][3] != 0: # alpha 0 roi_color[i+30, j] = glasses2[i, j] nose = nose_cascade.detectMultiScale( roi_gray, scaleFactor=1.5, minNeighbors=5) for (nx, ny, nw, nh) in nose: #cv2.rectangle(roi_color, (nx, ny), (nx + nw, ny + nh), (255, 0, 0), 3) roi_nose = roi_gray[ny: ny + nh, nx: nx + nw] mustache2 = image_resize(mustache.copy(), width=nw) mw, mh, mc = mustache2.shape
try: if not os.path.exists(img_dir): os.makedirs(img_dir) except OSError: print('Error: Creating directory of frames') now = datetime.datetime.now() finish_time = now + datetime.timedelta(seconds=seconds_duration) currentFrame = 0 frame_rate = 40 save_path = 'output.mp4' config = CFEVideoConf(in_vid, filepath=save_path, res='720p') out = cv2.VideoWriter(save_path, config.video_type, frame_rate, config.dims) while datetime.datetime.now() < finish_time: ret, frame = in_vid.read() filename = f"{img_dir}/{currentFrame}.jpg" currentFrame += 1 cv2.imwrite(filename, frame) time.sleep(seconds_between_shots) # Combine images to Video image_list = glob.glob(f"{img_dir}/*.jpg") for file in image_list: image_frame = cv2.imread(file) image = image_resize(image_frame, width=config.width) out.write(image) # All Release in_vid.release() out.release() cv2.destroyAllWindows()
scaleFactor=1.5, minNeighbors=5) frame = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA) for (x, y, w, h) in faces: roi_gray = gray[y:y + h, x:x + h] roi_color = frame[y:y + h, x:x + h] #cv2.rectangle(frame,(x,y),(x+w,y+h),(255,255,255),3) eyes = eyes_cascade.detectMultiScale(roi_gray, scaleFactor=1.5, minNeighbors=5) for (ex, ey, ew, eh) in eyes: #cv2.rectangle(roi_color, (ex, ey), (ey + eh, ex + ew), (127, 255, 0), 3) roi_eyes = roi_gray[ey:ey + eh, ex:ex + ew] glasses1 = image_resize(glasses.copy(), width=eh) gw, gh, gc = glasses1.shape for i in range(0, gw): for j in range(0, gh): #print(glasses[i,j]) if glasses1[i, j][3] != 0: roi_color[ey + i, ex + j] = glasses1[i, j] nose = nose_cascade.detectMultiScale(roi_gray, scaleFactor=1.5, minNeighbors=5) for (nx, ny, nw, nh) in nose: #cv2.rectangle(roi_color, (nx, ny), (nx + nw, ny + nh), (0, 255, 127), 3) roi_nose = roi_gray[ny:ny + nh, nx:nx + nw] mustache1 = image_resize(mustache.copy(), width=nw)
def main(): frame_interval = 3 # Number of frames after which to run face detection fps_display_interval = 5 # seconds frame_rate = 0 frame_count = 0 start_time = time.time() parser = get_parser() args = parser.parse_args() minsize = 20 # minimum size of face threshold = [0.6, 0.7, 0.7] # three steps's threshold factor = 0.709 # scale factor if bool(args.classifier) ^ bool(args.tf_graph_path): raise ValueError('tf_graph path and classifier must be filled.') use_classifier = args.classifier and args.tf_graph_path if args.camera: video_capture = cv2.VideoCapture(args.camera) else: video_capture = cv2.VideoCapture(0) bounding_boxes = [] labels = [] with tf.Session() as sess: pnet, rnet, onet = detect_face.create_mtcnn(sess, args.align_dir) if use_classifier: # Load classifier with open(args.classifier, 'rb') as f: opts = {'file': f} if six.PY3: opts['encoding'] = 'latin1' (model, class_names) = pickle.load(**opts) facenet.load_model(args.tf_graph_path) # Get input and output tensors images_placeholder = tf.get_default_graph().get_tensor_by_name( "input:0") embeddings = tf.get_default_graph().get_tensor_by_name( "embeddings:0") phase_train_placeholder = tf.get_default_graph( ).get_tensor_by_name("phase_train:0") try: while True: if not args.image: _, frame = video_capture.read() else: frame = cv2.imread(args.image) frame = utils.image_resize(frame, height=480) # frame = cv2.resize(frame, (640, 480), interpolation=cv2.INTER_AREA) # BGR -> RGB rgb_frame = frame[:, :, ::-1] if (frame_count % frame_interval) == 0: bounding_boxes, _ = detect_face.detect_face( rgb_frame, minsize, pnet, rnet, onet, threshold, factor) # Check our current fps end_time = time.time() if (end_time - start_time) > fps_display_interval: frame_rate = int(frame_count / (end_time - start_time)) start_time = time.time() frame_count = 0 if use_classifier: imgs = get_images(rgb_frame, bounding_boxes) labels = [] for img_idx, img in enumerate(imgs): img = img.astype(np.float32) feed_dict = { images_placeholder: [img], phase_train_placeholder: False } embedding = sess.run(embeddings, feed_dict=feed_dict) try: predictions = model.predict_proba(embedding) except ValueError as e: # Can not reshape print( "ERROR: Output from graph doesn't consistent" " with classifier model: %s" % e) continue best_class_indices = np.argmax(predictions, axis=1) best_class_probabilities = predictions[ np.arange(len(best_class_indices)), best_class_indices] for i in range(len(best_class_indices)): bb = bounding_boxes[img_idx].astype(int) text = '%.1f%% %s' % ( best_class_probabilities[i] * 100, class_names[best_class_indices[i]]) labels.append({ 'label': text, 'left': bb[0], 'top': bb[1] - 5 }) # DEBUG # print('%4d %s: %.3f' % ( # i, # class_names[best_class_indices[i]], # best_class_probabilities[i]) # ) add_overlays(frame, bounding_boxes, frame_rate, labels=labels) frame_count += 1 cv2.imshow('Video', frame) key = cv2.waitKey(1) # Wait 'q' or Esc if key == ord('q') or key == 27: break except (KeyboardInterrupt, SystemExit) as e: print('Caught %s: %s' % (e.__class__.__name__, e)) # When everything is done, release the capture video_capture.release() cv2.destroyAllWindows() print('Finished')
# Our operations on the frame come here gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, scaleFactor=1.5, minNeighbors=5) frame = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA) for (x, y, w, h) in faces: roi_gray = gray[y:y+h, x:x+w] #rec roi_color = frame[y:y+h, x:x+w] # cv2.rectangle(frame, (x,y), (x+w, y+h), (255,255,255), 2) eyes = eyes_cascade.detectMultiScale(roi_gray, scaleFactor=1.5, minNeighbors=5) for(ex, ey, ew, eh) in eyes: # cv2.rectangle(roi_color, (ex,ey), (ex+ew, ey+eh), (0,255,0), 2) roi_eyes = roi_gray[ey:ey+eh, ex:ex+ew] glasses2 = image_resize(glasses.copy(), width=ew) gw, gh, gc = glasses2.shape for i in range(0, gw): for j in range(0, gh): #print(glasses[i, j]) #RGBA if glasses2[i, j][3] != 0: # alpha 0 roi_color[ey + i, ex + j] = glasses2[i, j] # smile = smile_cascade.detectMultiScale(roi_gray, scaleFactor=1.5, minNeighbors=5) # for(sx, sy, sw, sh) in smile: # # cv2.rectangle(roi_color, (ex,ey), (ex+ew, ey+eh), (0,255,0), 2) # roi_eyes = roi_gray[sy:sy+sh, sx:sx+sw] # smile2 = image_resize(smile_logo.copy(), width=sw)
scaleFactor=1.5, minNeighbors=5) frame = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA) for (x, y, w, h) in faces: roi_gray = gray[y:y + h, x:x + h] # rectangle roi_color = frame[y:y + h, x:x + h] # cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 255, 255), 3) eyes_detection = eyes_cascade.detectMultiScale(roi_gray, scaleFactor=1.5, minNeighbors=5) for (ex, ey, ew, eh) in eyes_detection: # cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 3) roi_eyes = roi_gray[ey:ey + eh, ex:ex + ew] glasses = image_resize(glasses, width=ew) gw, gh, gc = glasses.shape for i in range(0, gw): for j in range(0, gh): # print(glasses[i, j]) if glasses[i, j][3] != 0: roi_color[ey + i, ex + j] = glasses[i, j] nose_detection = nose_cascade.detectMultiScale(roi_gray, scaleFactor=1.5, minNeighbors=5) for (nx, ny, nw, nh) in nose_detection: # cv2.rectangle(roi_color, (nx, ny), (nx + nw, ny + nh), (255, 0, 0), 3) roi_eyes = roi_gray[ny:ny + nh, nx:nx + nw]
def start_downloading(position=0): current_image = 0 for i in range(len(file_images)): if (i >= position ): # Если достигли нужной позиции продолжения скачивания try: recipe_to_db = Recipe( id=i + 1, calories=file_calories[i], total_time=file_total_time[i], keywords=file_keywords[i], ingredients=file_ingredients[i], ingredients_quantity=file_ingredients_quantity[i], fat_content=file_fat_content[i], saturated_fat_content=file_saturated_fat_content[i], cholesterol_content=file_cholesterol_content[i], sodium_content=file_sodium_content[i], carbohydrate_content=file_carbohydrate_content[i], fiber_content=file_fiber_content[i], sugar_content=file_sugar_content[i], protein_content=file_protein_content[i], instructions=file_instructions[i], name=file_name[i]) db_session.add(recipe_to_db) db_session.commit() for j in range(len(file_images[i])): temp_path = f"Pictures/{file_categories[i].replace('/','_')}/image{current_image + 1}.jpg" try: if not os.path.exists(os.path.dirname(temp_path)): os.makedirs(os.path.dirname(temp_path)) time.sleep(0.01) urlretrieve(file_images[i][j], temp_path) img = Image.open(temp_path) img = image_resize(img) img.save(temp_path) img.close() except: if os.path.exists(temp_path): os.remove(temp_path) else: image_to_db = FoodImage(id=current_image + 1, recipe_id=i + 1) db_session.add(image_to_db) db_session.commit() print( f'{current_image} / {total_images} ({round(current_image/total_images*100, 1)}%)' ) current_image += 1 if current_image % 1000 == 0: time.sleep(10) if current_image % 20000 == 0: time.sleep(120) except: try: for j in range( len(file_images[i]) ): #Если не удалось скачать изображение текущий индекс все равно увеличится current_image += 1 except: pass continue else: #Если не достигли нужной позиции рецепта try: for j in range(len(file_images[i])): # Проход по рецепту current_image += 1 # Увеличиваем текущий индекс except: pass
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA) if c==1: for (x, y, w, h) in faces: roi_gray = gray[y:y+h, x:x+h] roi_color = frame[y:y+h, x:x+h] #cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 255, 255), 3) eyes = eyes_cascade.detectMultiScale(roi_gray, scaleFactor=1.3, minNeighbors=5) for (ex, ey, ew, eh) in eyes: #cv2.rectangle(roi_color, (ex, ey), ((ex + ew), ey + eh), (0, 255, 0), 3) roi_eyes = roi_gray[ey: ey + eh, ex: (ex + ew)] glasses2 = image_resize(glasses.copy(), width=2*ew) gw, gh, gc = glasses2.shape for i in range(0, gw): for j in range(0, gh): #print(glasses[i, j]) if glasses2[i, j][3] != 0: roi_color[ey + i, ex + j] = glasses2[i, j] nose = nose_cascade.detectMultiScale(roi_gray, scaleFactor=1.5, minNeighbors=5) for (nx, ny, nw, nh) in nose: #cv2.rectangle(roi_color, (nx, ny), (nx + nw, ny + nh), (255, 0, 0), 3) roi_nose = roi_gray[ny: ny + nh, nx: nx + nw] mustache2 = image_resize(mustache.copy(), width=nw)