def init(): """Get and parse parameters from console""" args = sys.argv[1:] if len(args) == 0: logger.error("-p can't be empty") raise ValueError("-p can't be empty") logger.debug(f"run with params: {args}") # transform arguments from console opts, rem = getopt.getopt( args, "p:", ["rotate=", "resize=", "color_filter=", "flip_top", "flip_left"]) rotate_angle = resize = color_filter = flip_top = flip_left = None path = None for opt, arg in opts: if opt == "-p": path = arg elif opt == "--rotate": rotate_angle = int(arg) elif opt == "--resize": resize = arg elif opt == "--color_filter": color_filter = arg elif opt == "--flip_top": flip_top = True elif opt == "--flip_left": flip_left = arg if not path: raise ValueError("No path") img = img_helper.get_img(path) if rotate_angle: img = img_helper.rotate(img, rotate_angle) if resize: w, h = map(int, resize.split(',')) img = img_helper.resize(img, w, h) if color_filter: img = img_helper.color_filter(img, color_filter) if flip_left: img = img_helper.flip_left(img) if flip_top: img = img_helper.flip_top(img) if __debug__: img.show()
def on_capture(self): cam = VideoCapture(0) # 0 -> index of camera s, img = cam.read() if s: # frame captured without any errors namedWindow("camcapture") imshow("camcapture",img) waitKey(0) destroyWindow("camcapture") imwrite("capture.jpg",img) #save image logger.debug("capture") img_path = 'capture.jpg' if img_path: logger.debug(f"open file {img_path}") self.file_name = ntpath.basename(img_path) pix = QPixmap(img_path) self.img_lbl.setPixmap(pix) self.img_lbl.setScaledContents(False) self.action_tabs.setVisible(True) self.action_tabs.adjustment_tab.reset_sliders() global _img_original _img_original = ImageQt.fromqpixmap(pix) self.update_img_size_lbl() if _img_original.width < _img_original.height: w = THUMB_SIZE h = _get_ratio_height(_img_original.width, _img_original.height, w) else: h = THUMB_SIZE w = _get_ratio_width(_img_original.width, _img_original.height, h) img_filter_thumb = img_helper.resize(_img_original, w, h) global _img_preview _img_preview = _img_original.copy() for thumb in self.action_tabs.filters_tab.findChildren(QLabel): if thumb.name != "none": img_filter_preview = img_helper.color_filter(img_filter_thumb, thumb.name) else: img_filter_preview = img_filter_thumb preview_pix = ImageQt.toqpixmap(img_filter_preview) thumb.setPixmap(preview_pix) self.reset_btn.setEnabled(True) self.save_btn.setEnabled(True) self.action_tabs.modification_tab.set_boxes()
def on_filter_select(self, filter_name, e): logger.debug(f"apply color filter: {filter_name}") global _img_preview if filter_name != "none": _img_preview = img_helper.color_filter(_img_original, filter_name) else: _img_preview = _img_original.copy() operations.color_filter = filter_name self.toggle_thumbs() self.parent.parent.place_preview_img()
def on_upload(self): global img_path logger.debug("upload") img_path, _ = QFileDialog.getOpenFileName(self.parent, "Open image", "/Users", "Images (*.png *jpg)") if img_path: logger.debug(f"open file {img_path}") self.file_name = ntpath.basename(img_path) pix = QPixmap(img_path) self.img_lbl.setPixmap(pix) self.img_lbl.setScaledContents(True) self.action_tabs.setVisible(True) self.action_tabs.adjustment_tab.reset_sliders() global _img_original _img_original = ImageQt.fromqpixmap(pix) self.update_img_size_lbl() if _img_original.width < _img_original.height: w = THUMB_SIZE h = _get_ratio_height(_img_original.width, _img_original.height, w) else: h = THUMB_SIZE w = _get_ratio_width(_img_original.width, _img_original.height, h) img_filter_thumb = img_helper.resize(_img_original, w, h) global _img_preview _img_preview = _img_original.copy() for thumb in self.action_tabs.filters_tab.findChildren(QLabel): if thumb.name != "none": img_filter_preview = img_helper.color_filter( img_filter_thumb, thumb.name) else: img_filter_preview = img_filter_thumb preview_pix = ImageQt.toqpixmap(img_filter_preview) thumb.setPixmap(preview_pix) self.reset_btn.setEnabled(True) self.save_btn.setEnabled(True) self.action_tabs.modification_tab.set_boxes()
def on_upload(self, img_path): logger.debug("upload") if img_path: logger.debug("open file " + img_path) imageUri = QtCore.QUrl( QtCore.QString("file://{0}".format(img_path))) self.file_name = ntpath.basename(str(img_path)) pix = QPixmap(str(img_path)) self.img_lbl.setPixmap(pix) self.img_lbl.setScaledContents(True) self.action_tabs.setVisible(True) self.action_tabs.adjustment_tab.reset_sliders() global _img_original _img_original = ImageQt.fromqpixmap(pix) self.update_img_size_lbl() logger.debug("_img_original.width " + str(_img_original.width)) logger.debug("_img_original.height " + str(_img_original.height)) h = THUMB_SIZE w = THUMB_SIZE if _img_original.width < _img_original.height: w = THUMB_SIZE h = _get_ratio_height(_img_original.width, _img_original.height, w) elif _img_original.width > _img_original.height: h = THUMB_SIZE w = _get_ratio_width(_img_original.width, _img_original.height, h) img_filter_thumb = img_helper.resize(_img_original, w, h) global _img_preview _img_preview = _img_original.copy() for thumb in self.action_tabs.filters_tab.findChildren(QLabel): if thumb.name != "none": img_filter_preview = img_helper.color_filter( img_filter_thumb, thumb.name) else: img_filter_preview = img_filter_thumb preview_pix = ImageQt.toqpixmap(img_filter_preview) thumb.setPixmap(preview_pix) self.reset_btn.setEnabled(True) self.save_btn.setEnabled(True) self.action_tabs.modification_tab.set_boxes()
def place_preview_img(self): if not (operations.color_filter == None or operations.color_filter == 'none'): img = _get_img_with_all_operations() img = img_helper.color_filter(img, operations.color_filter) else: img = _get_img_with_all_operations() preview_pix = ImageQt.toqpixmap(img) self.img_lbl.setPixmap(preview_pix) print("Canvas Updated!!")
def on_save(self): logger.debug("open save dialog") new_img_path, _ = QFileDialog.getSaveFileName( self.parent, "QFileDialog.getSaveFileName()", f"ez_pz_{self.file_name}", "Images (*.png *.jpg)") if new_img_path: logger.debug(f"save output image to {new_img_path}") if not (operations.color_filter == None or operations.color_filter == 'none'): img = _get_img_with_all_operations() img = img_helper.color_filter(img, operations.color_filter) else: img = _get_img_with_all_operations() img.save(new_img_path)
def mousePressEvent(self, event): if self._photo.isUnderMouse() and self.parent.captureMouseClick: print(self.mapToScene(event.pos()).toPoint()) x = self.mapToScene(event.pos()).toPoint().x() y = self.mapToScene(event.pos()).toPoint().y() global _img_preview im = np.array(_img_preview) im2 = im[:, :, :] arr = im[y, x, :] _img_preview = img_helper.color_filter(_img_preview, "gray") im = np.array(_img_preview) im[:, :] = np.where(im2[:, :] == arr, arr, im[:, :]) _img_preview = Image.fromarray(im) preview_pix = ImageQt.toqpixmap(_img_preview) self.setPhoto(preview_pix) self.photoClicked.emit(self.mapToScene(event.pos()).toPoint()) self.parent.captureMouseClick = False super(PhotoViewer, self).mousePressEvent(event)
def load_image(self, img_path): self.viewer.setPhoto(QtGui.QPixmap(img_path)) self._empty = False logger.debug(f"open file {img_path}") self.name = img_path print(self.name) global _img_path _img_path = self.name pix = QPixmap(img_path) self.action_tabs.setVisible(True) self.action_tabs.adjustment_tab.reset_sliders() self.action_tabs.histogram_tab.reset_sliders() global _img_original _img_original = ImageQt.fromqpixmap(pix) if _img_original.width < _img_original.height: w = THUMB_SIZE h = _get_ratio_height(_img_original.width, _img_original.height, w) else: h = THUMB_SIZE w = _get_ratio_width(_img_original.width, _img_original.height, h) img_filter_thumb = img_helper.resize(_img_original, w, h) global _img_preview _img_preview = _img_original.copy() for thumb in self.action_tabs.filters_tab.findChildren(QLabel): if thumb.name != "none": img_filter_preview = img_helper.color_filter( img_filter_thumb, thumb.name) else: img_filter_preview = img_filter_thumb preview_pix = ImageQt.toqpixmap(img_filter_preview) thumb.setPixmap(preview_pix) self.action_tabs.modification_tab.set_boxes()
def interpreter(self, s, img, img_path=''): if s.data == 'tuning': tmp = 1 if s.children[0] == 'increase' else -1 attr = s.children[1] val = int(s.children[2]) print(tmp, attr, val) elif s.data == 'set_filter': filter_name = s.children[0] return img_helper.color_filter(img, filter_name) elif s.data == 'flip': flip_dir = s.children[0] if flip_dir == 'vertically': return img_helper.flip_top(img) elif flip_dir == 'horizontally': return img_helper.flip_left(img) elif s.data == 'detect': print(img_path) if self.yolo is None: self.yolo = Yolo() self.agender = Agender() self.emotion = Emotion() if img_path != self.img_path: self.img_path = img_path self.persons = [] self.objects = [] objects = self.yolo.detect(img_path) faces = self.agender.detect(img_path) emotions = self.emotion.detect(img_path) font_path = "/Users/woffee/www/language_design/prototype/font_consolas/CONSOLA.TTF" font = ImageFont.truetype(font_path, 20) fw = 11 fh = 16 msg = [] draw = ImageDraw.Draw(img) for i, box in enumerate(objects): l = box['left'] t = box['top'] b = box['bottom'] r = box['right'] label = box['class'] self.objects.append({ 'left': l, 'top': t, 'bottom': b, 'right': r, 'class': label }) if label == 'person': self.persons.append({ 'left': l, 'top': t, 'bottom': b, 'right': r, 'gender': '', 'age': 0, 'emotion': '' }) else: draw.rectangle(((l, t), (r, b)), outline='blue') txt_width = fw * len(label) draw.rectangle(((l, t), (l + txt_width, t + fh)), fill="blue") draw.text((l, t), label, font=font) for i, box in enumerate(faces): l = box['left'] t = box['top'] b = box['bottom'] r = box['right'] gender = 'male' if box['gender'] < 0.5 else 'female' age = box['age'] label = gender + ", %.2f" % age print(" * Agender: " + label) # msg.append(" * Agender: " + label) score = 0 for i, p in enumerate(self.persons): area = self.computeArea(l, t, r, b, p['left'], p['top'], p['right'], p['bottom']) s = area / ((r - l) * (b - t)) if s > 0.5: self.persons[i]['age'] = age self.persons[i]['gender'] = gender for i, box in enumerate(emotions): l = box['left'] t = box['top'] b = box['bottom'] r = box['right'] emo = box['emotion'] print(" * Emotion: " + emo) # msg.append(" * Emotion: " + emo) for i, p in enumerate(self.persons): area = self.computeArea(l, t, r, b, p['left'], p['top'], p['right'], p['bottom']) if (r - l) * (b - t) > 0: s = area / ((r - l) * (b - t)) if s > 0.5: self.persons[i]['emotion'] = emo # draw.rectangle(((l, t), (r, b)), outline='yellow') # txt_width = fw * len(emo) # draw.rectangle(((l, t), (l + txt_width, t + fh)), fill="black") # draw.text((l, t), emo, font=font) for i, box in enumerate(self.persons): l = box['left'] t = box['top'] b = box['bottom'] r = box['right'] draw.rectangle(((l, t), (r, b)), outline='blue') gender_age = box['gender'] if box['age'] > 0: gender_age = gender_age + ", age %.2f" % box['age'] emotion = box['emotion'] txt_width = fw * len(gender_age) draw.rectangle(((l, t), (l + txt_width, t + fh)), fill="blue") draw.text((l, t), gender_age, font=font) txt_width = fw * len(emotion) draw.rectangle(((l, t + fh), (l + txt_width, t + fh * 2)), fill="blue") draw.text((l, t + fh), emotion, font=font) self.msg = " * done" elif s.data == 'how_many': str = s.children[0] str = self.remove_s(str) num = 0 for obj in self.objects: if obj['class'] == str: num += 1 self.msg = " * %d %s(s)" % (num, str) elif s.data == 'tag_objects': img = self.original_img.copy() str = s.children[0] str = self.remove_s(str) msg = [] draw = ImageDraw.Draw(img) font_path = "/Users/woffee/www/language_design/prototype/font_consolas/CONSOLA.TTF" font = ImageFont.truetype(font_path, 20) fw = 11 fh = 16 for i, box in enumerate(self.objects): if str == box['class']: l = box['left'] t = box['top'] b = box['bottom'] r = box['right'] label = box['class'] print(" * Yolo: " + label) msg.append(" * Yolo: " + label) draw.rectangle(((l, t), (r, b)), outline='blue') txt_width = fw * len(label) draw.rectangle(((l, t), (l + txt_width, t + fh)), fill="blue") draw.text((l, t), label, font=font) self.msg = "\n".join(msg) elif s.data == 'show_statistics': statistics = {} for o in self.objects: if o['class'] not in statistics.keys(): statistics[o['class']] = 1 else: statistics[o['class']] += 1 msg = [] for k in statistics.keys(): v = statistics[k] msg.append(" * %d %s(s)" % (v, k)) self.msg = "\n".join(msg) elif s.data == 'detect_food': self.detect_food(img_path) elif s.data == 'image_segmentation': img = self.image_segmentation(img_path) return img