def __getitem__(self, i): random_seed = random.randint(0, 3) random_seed = self.filp_dict[random_seed] data = self.data[i] img = cv2.imread(data[0]) mask = np.zeros_like(img[:, :, 0]) if data[1]: mask = gif2numpy.convert(data[1]) mask = mask[0][0] # mask_out = mask[:, :, 0] == 255 mask_out = np.zeros_like(mask[:, :, 0]) mask_out[mask[:, :, 0] == 255] = 1 mask = mask_out # if self.set_mode=='train': # mask = self.process_data(mask,random_seed) # mask = mask[224:448,224:448] # img = self.preprocess(img, self.scale) # mask = self.preprocess(mask, self.scale) # img= img[224:448,224:448,:] # img_out = img # img = self.process_data(img,random_seed) img_out = cv2.resize(img, (448, 448)) # cv2.imshow('a',img_out) # cv2.waitKey() # cv2.destroyAllWindows() img_out = img_out / 255 # mean = (img_out).mean((0,1)) # img_out = img_out - mean return { 'image': torch.from_numpy(img_out).permute((2, 0, 1)), 'mask': torch.from_numpy(np.array(mask)), 'o_s': img.shape[:2] }
def analyze_color(image_path): if not image_path.endswith('.gif'): im = cv2.imread(image_path) else: np_images, extensions, image_specs = gif2numpy.convert(image_path) im = np_images[0] distinct_colors = set(tuple(v) for m2d in im for v in m2d) color_dict[image_path] = len(distinct_colors)
def get_all(self): data = [] mm = [] for d in self.data: img = cv2.imread(d[0]) if data[1]: mask = gif2numpy.convert(d[1]) mask = mask[0][0] mask = mask[:, :, 0] == 255 mm.append(mask) data.append(img) data = np.array(data) mm = np.array(mm) return data, mm
def getImages(directorypath): os.chdir(directorypath) images = [] # Gather the images in the file path for file in glob.glob("*"): # cv2 doesn't suppoer gif so we are converting all gifs to be numpy compatible if file.endswith(".gif"): frames, extensions, image_specs = gif2numpy.convert(file) image = frames[0] else: image = cv2.imread(file, 1) images.append(image) # Resize images to be the same size as the first width = int(0.6 * images[0].shape[1]) height = int(0.6 * images[0].shape[0]) dim = (width, height) for i, img in enumerate(images): images[i] = cv2.resize(img, dim, interpolation=cv2.INTER_AREA) return images
def testGenerator(test_image_path, test_label_path, img_size, img_shape): # read the list of test image in dir image_list = os.listdir(test_image_path) image_list = sorted(image_list) print(image_list) test_test_data = [] test_label_data = [] for img_name in image_list: #print(test_image_path+img_name) img = cv2.imread(test_image_path + img_name, 1) img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # resize to 512, 512 img = cv2.resize(img, img_size) img = img.astype('float32') img = img / 255. img = np.reshape(img, img_shape) test_test_data.append(img) label_list = os.listdir(test_label_path) label_list = sorted(label_list) print(label_list) for img_name in label_list: # read gif image np_frames, extensions, img_spec = gif2numpy.convert(test_label_path + img_name) #img = cv2.imread(test_label_path+img_name,1) #img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # resize to 512, 512 #img = cv2.resize(img,img_size) img = cv2.resize(np_frames[0], img_size) #img = img.astype('float32') img = img / 255. img = img[:, :, 0] img = np.reshape(img, img_shape) test_label_data.append(img) return ([test_test_data, test_label_data])
# cv2.waitKey() # # cv2.destroyAllWindows() return image_segmented if __name__ == '__main__': output_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'extras') data_path = glob.glob(os.path.join(os.path.dirname(__file__), 'data', '*')) data = {} for d in data_path: if 'manual1' in d: gif = gif2numpy.convert(d) continue elif '02' not in d: continue data[d.split('/')[-1]] = cv2.imread(d) print(d) img = list(data.values())[0] # cv2.imshow('Origin', img) # cv2.waitKey() # cv2.destroyAllWindows() res = seg(img) cv2.imwrite(os.path.join(output_dir, 'fused_res_02.jpg'), res) pass
import gif2numpy import numpy as np import cv2 np_frame, extenstions, image_spec = gif2numpy.convert("white.gif") #for fram in np_frame: s = "" l = "" board = np.zeros(shape=(100, 100, 3)) x = 50 y = 50 i = 0 boards = [] for frame in np_frame: out = False for row, r in zip(frame, range(len(frame))): for col, c in zip(row, range(len(row))): if (not (out) and all(x == 8 for x in col)): if (r == 100 and c == 100): i += 1 boards.append(board) board = np.zeros(shape=(100, 100, 3)) x = 50 y = 50 out = True if (r == 100 and c == 102): x += 1 if (r == 102 and c == 100): y += 1 if (r == 102 and c == 102): x += 1 y += 1
def main(dir: str): checker = language_tool_python.LanguageTool('en-US') emails = {} totalWords = '' filenames = [ filename for filename in os.listdir(dir) if filename.endswith('.eml') ] for filename in filenames: print() print('[INFO] Processing {}...'.format(filename)) with open(os.path.join(dir, filename), 'r', encoding='latin1') as file: try: mail = mailparser.parse_from_file_obj(file) except Exception as e: print('[WARNING] Error while parsing: {}'.format(e)) continue # filter duplicates based on subject #if mail.subject in emails: # print('[WARNING] This email seems to be a duplicate of "{}"! Skipping...' # .format(emails[mail.subject]['filename'])) # continue # don't process if auth results missing # if 'Authentication-Results' not in mail.headers: # print('[WARNING] This email is missing an authentication results header! Skipping...') # continue attachments = '' for attachment in mail.attachments: attachment['filename'] = re.sub(r'<|>', '', attachment['filename']) try: mail.write_attachments(dir) for attachment in mail.attachments: if re.search('image', attachment['mail_content_type']): if re.search('gif', attachment['mail_content_type']): images, _, _ = gif2numpy.convert( dir + '\\' + attachment['filename']) img = images[0] else: img = cv2.imread(dir + '\\' + attachment['filename']) img = cv2.resize(img, None, fx=1.2, fy=1.2, interpolation=cv2.INTER_CUBIC) text = pytesseract.image_to_string(img) attachments += text elif re.search('pdf', attachment['mail_content_type']): encoding = chardet.detect( pdf_to_text(dir + '\\' + attachment['filename']))['encoding'] attachments += pdf_to_text( dir + '\\' + attachment['filename']).decode(encoding) # elif re.search('text', attachment['mail_content_type']): # #print(chardet.detect((attachment['payload']).encode())) # #encoding = chardet.detect(base64.b64decode(attachment['payload']).encode())['encoding'] # #attachments += base64.b64decode(attachment['payload']).decode(encoding) # #print(codecs.encode(base64.b64decode(attachment['payload']), encoding=attachment['content_transfer_encoding'])) # attachments += attachment['payload'] else: attachments += attachment['payload'] os.remove(dir + '\\' + attachment['filename']) except Exception as e: print( '[WARNING] Error while parsing attachments: {}'.format(e)) [ os.remove(dir + '\\' + attachment['filename']) for attachment in mail.attachments ] body = mail.subject + ' ' + \ remove_noise(BeautifulSoup(mail.body, 'lxml').get_text(separator=' ', strip=True) + BeautifulSoup(attachments, 'lxml').get_text()) blob = TextBlob(body) totalWords = totalWords + " " + body.lower() grammarErrors = checker.check(body) if 'Authentication-Results' in mail.headers: spf = re.findall('spf=(\S*)', mail.headers['Authentication-Results']) dkim = re.findall('dkim=(\S*)', mail.headers['Authentication-Results']) dmarc = re.findall('dmarc=(\S*)', mail.headers['Authentication-Results']) else: spf = dkim = dmarc = '' emails[filename] = { 'filename': filename, # 'hops': mail.received[-1]['hop'], # 'totalDelay': sum([hop['delay']/60 for hop in mail.received]), 'spf': spf[0] if len(spf) else None, 'dkim': dkim[0] if len(dkim) else None, 'dmarc': dmarc[0] if len(dmarc) else None, 'subject': mail.subject, 'from': mail.from_[0][1], 'to': [tup[1] for tup in mail.to], 'replyTo': [tup[1] for tup in mail.reply_to], 'attachments': [x['filename'] for x in mail.attachments], 'grammarErrors': len(grammarErrors), 'counts': { 'characterCount': len(body), 'wordCount': textstat.lexicon_count(body), 'sentenceCount': textstat.sentence_count(body) }, 'readability': { 'flesch_kincaid': textstat.flesch_kincaid_grade(body), 'gunning_fog': textstat.gunning_fog(body), 'smog_index': textstat.smog_index(body), 'automated_readability_index': textstat.automated_readability_index(body), 'coleman_liau_index': textstat.coleman_liau_index(body), 'linsear_write': textstat.linsear_write_formula(body), }, 'sentiment': { 'polarity': blob.sentiment.polarity, 'subjectivity': blob.sentiment.subjectivity } } if save_body: emails[filename]['body'] = body ## quit if nothing found ## # if not emails: # print('[WARNING] No files were found in "{}"!'.format(dir)) # return ## writing all words to file ## with open(os.path.join(dir, 'words.txt'), 'w', encoding='utf-8') as file: file.write(totalWords.lower()) ## output json ## with open(os.path.join(dir, 'analysis.json'), 'w') as jsonFile: json.dump(emails, jsonFile, indent=2) ## build and output csv ## # generate and output headers using first email column_headers = list(flatten_json(emails[list(emails.keys())[0]]).keys()) csvFile = open(os.path.join(dir, 'analysis.csv'), 'w', encoding='utf-8') csvFile.write(',{}\n'.format(','.join(column_headers))) # generate and output one line per email for email in emails.keys(): # flatten json to 1 layer deep flattened_email = flatten_json(emails[email]) # generate the values for this row csv_values = [ '"' + str(flattened_email[column_header]) + '"' for column_header in column_headers ] # add email name and join w/ commas, then write out csvFile.write('{},{}\n'.format('"' + email + '"', ','.join(csv_values))) csvFile.close() # print out stats print('{}/{} processed. The remaining failed for some reason.'.format( len(emails), len(filenames)))
import gif2numpy import cv2 import numpy as np from PIL import Image def rgb2gray(rgb): r, g, b = rgb[:, :, 0], rgb[:, :, 1], rgb[:, :, 2] gray = 0.2989 * r + 0.5870 * g + 0.1140 * b return np.array(gray).astype('int') np_frames, extensions, image_specifications = gif2numpy.convert("alphabet.gif") img = np_frames[0] for i in range(10): #cv2.imshow(chr(65 + i), img[0:44, i*43:(i+1)*43+1, :]) cv2.imwrite( chr(48 + (i + 1) % 10) + '.jpg', img[43 * 4:43 * 5 + 1, i * 43:(i + 1) * 43 + 1, :]) cv2.waitKey() cv2.destroyAllWindows()
def readGIF(path): import gif2numpy np_imgs, extensions, img_specs = gif2numpy.convert(path) return np_imgs[0]
from __future__ import print_function from gif2numpy import convert from numpy2gif import write_gif import cv2 image = "Images/Rotating_earth.gif" frames, exts, image_specs = convert(image, BGR2RGB=False) print("len(frames), len(exts), exts[0]['delay_time']", len(frames), len(exts), exts[0]['delay_time']) write_gif(frames, 'Rotating_earth_new.gif', fps=12) write_gif(frames[0], 'earth.gif')
from __future__ import print_function import gif2numpy import cv2 print(gif2numpy.version) images = "Images/hopper.gif", "Images/audrey.gif", "Images/Rotating_earth.gif", "Images/testcolors.gif" for image in images: frames, exts, image_specs = gif2numpy.convert(image) print() print("Image:", image) print() print("len frames", len(frames)) print("len exts", len(exts)) print("exts:", exts) print("image_specs:", image_specs) for i in range(len(frames)): cv2.imshow("np_image", frames[i]) print(exts[i]) k = cv2.waitKey(0) if k == 27: break cv2.destroyWindow("np_image")