def test_jpeg_rotation(self): # Make sure all Portrait test images are auto-rotated correctly for i in range(9): img_jpg = load_image_file(f"Portrait_{i}.jpg") ref_img = np.load(f"Portrait_{i}.jpg.npy") self.assertTrue(np.array_equal(ref_img, img_jpg)) # Make sure all Landscape test images are auto-rotated correctly for i in range(9): img_jpg = load_image_file(f"Landscape_{i}.jpg") ref_img = np.load(f"Landscape_{i}.jpg.npy") self.assertTrue(np.array_equal(ref_img, img_jpg))
def analyze_color(image_path, candidates=3): # Reading Image file img = image_to_numpy.load_image_file(image_path) img = cv2.resize(img, (180, 180)) ar = np.asarray(img) # Collapse image to rgb array ar = np.reshape(ar, newshape=(-1, ar.shape[-1])).astype(float) # print('finding clusters') codes, dist = kmeans(ar, NUM_CLUSTERS) # print('cluster centres:\n', codes) vecs, dist = vq(ar, codes) # assign codes counts, bins = np.histogram(vecs, len(codes)) # count occurrences sorted_idx = np.argsort(counts)[::-1] res = [] for i in range(candidates): peak = codes[sorted_idx[i]] res += [ntc.name(peak)] return res
def kakao_ocr(image_path: str, api_key: str): """ OCR api request example :param image_path: 이미지파일 경로 :param api_key: 카카오 앱 REST API 키 """ resize_impath = kakao_ocr_resize(image_path) if resize_impath is not None: image_path = resize_impath headers = {'Authorization': 'KakaoAK {}'.format(api_key)} image = image_to_numpy.load_image_file(image_path) image = cv2.imencode('.png', image)[1] data = image.tobytes() api_response = requests.post(API_URL, headers=headers, files={ "image": data }).json() print(api_response) if resize_impath is not None: os.remove(resize_impath) if len(api_response['result']) == 0: return -1 result = [ token['recognition_words'][0] for token in api_response['result'] ] return ' '.join(result)
def is_valid(self): if self.__validated is not None: return self.__validated try: if self.url: req = request.urlopen(self.url) self.image = image_to_numpy.load_image_file(BytesIO(req.read())) elif self.image: base64_data = re.sub('^data:image/.+;base64,', '', self.image) byte_data = base64.b64decode(base64_data) image_data = BytesIO(byte_data) self.image = image_to_numpy.load_image_file(image_data) self.__validated = True except Exception as e: self.__validated = False return self.__validated
def load_image(image_path): # img = tf.io.read_file(image_path) # img = tf.image.decode_jpeg(img, channels=3) img = image_to_numpy.load_image_file(image_path) # plt.imshow(img) # plt.show() img = tf.image.resize(img, (380, 380)) # (380, 380) for efficient-netB4 img = tf.keras.applications.efficientnet.preprocess_input(img) return img, image_path
def test_png(self): # Can we load a non-jpeg file with no metadata? img_png = load_image_file("Portrait_8.png") self.assertEqual(img_png.shape, (1800, 1200, 3))
def test_jpeg_no_exif(self): # Can we load a jpeg with no metadata without crashing? img_jpg = load_image_file("Portrait_no_exif.jpg") self.assertEqual(img_jpg.shape, (1200, 1800, 3))
COLOR = [0, 0, 255] DETECTION_MODEL = "hog" log = Logger(os.path.basename(__file__)) source_faces = [] start = time.perf_counter() """LOADING SOURCE IMAGES""" log.info(f"loading source images") for index, filename in enumerate(os.listdir(SOURCE_DATA_DIR)): if filename.endswith("JPG"): log.info(f"processing source image {filename}") img_path = SOURCE_DATA_DIR + filename # using image_to_numpy to load img file -> fixes image orientation -> face encoding is found img = image_to_numpy.load_image_file(img_path) try: source_img_encoding = face_recognition.face_encodings(img)[0] log.success("face encoding found") source_faces.append(source_img_encoding) except IndexError: log.error("no face detected") if (len(os.listdir(SOURCE_DATA_DIR)) - 1) - len(source_faces) != 0: # -1 for .gitignore log.warn( f"{str(len(source_faces))} faces found in {str(len(os.listdir(SOURCE_DATA_DIR)))} images" ) """MAIN PROCESS""" log.info(f"Processing dataset") for index, filename in enumerate(os.listdir(IMAGE_SET_DIR)):
def main(): #----------Load Known values----------# known_encodings = [] #arrays for encodings and associated names known_names = [] for filename in os.listdir("./images"): #loop through images folder for known names and images if filename.startswith("."): #ignore .ds_store and other hiddens continue image_file = fr.load_image_file("./images/" + filename) image_encodings = fr.face_encodings(image_file)[0] known_encodings.append(image_encodings) name = filename.split(".")[0] #better hope there are no periods in name known_names.append(name) face_locations, face_encodings, face_names = [], [], [] #arrays for processing sent frames process_frame = 0 #counter to allow processing every n frames n = 1 #----------Process Captured Frames----------# for fname in os.listdir("./captured_frames"): #loop through frames stored in captured_frames folder if fname.startswith("."): #ignore .ds_store and other hiddens continue frame = im_np.load_image_file("./captured_frames/" + fname) RESIZE = 2 #resize for faster processing small_frame = cv2.resize(frame, (0,0), fx=1/RESIZE, fy=1/RESIZE) rgb_small = small_frame[:, :, ::-1] #process every few frames if process_frame % n == 0: face_locations = fr.face_locations(rgb_small) face_encodings = fr.face_encodings(rgb_small, face_locations) #check each set of encodings for a match for face_encoding in face_encodings: matches = fr.compare_faces(known_encodings, face_encoding) name = "Unknown" #get "closest" match face_distances = fr.face_distance(known_encodings, face_encoding) best_match_index = np.argmin(face_distances) if matches[best_match_index]: name = known_names[best_match_index] #append name to list if not (name in face_names): face_names.append(name) process_frame += 1 tot_names = len(face_names) if tot_names > 0: #append names to response string response = "I have found " c = 1 for name in face_names: response += name if c < tot_names: response += " and " c += 1 print(response) else: print("I have found no one")