Exemple #1
0
def annotations(ctx, proc_img_path, proc_img_url, save_path, model_path,
                threshold_score, model_type):
    ''' Save processed image annotations '''
    from maskrcnn_modanet import processimages

    segments = True
    all_set = False
    if (not segments or
        (segments and not all_set)) and ((1 if proc_img_path else 0) +
                                         (1 if proc_img_url else 0) +
                                         (1 if all_set else 0)) == 1:
        model, labels_to_names = processimages.loadModel(model_type=model_type,
                                                         model_path=model_path)
        processimages.main(proc_img_path,
                           proc_img_url,
                           False,
                           save_path,
                           model_path,
                           segments,
                           True,
                           threshold_score,
                           model=model,
                           labels_to_names=labels_to_names)
    else:
        print_help(ctx, None, value=True)
Exemple #2
0
def image(ctx, proc_img_path, proc_img_url, save_path, segments, all_set,
          model_path, threshold_score, limit):
    ''' Save processed image '''
    if (not segments or
        (segments and not all_set)) and ((1 if proc_img_path else 0) +
                                         (1 if proc_img_url else 0) +
                                         (1 if all_set else 0)) == 1:
        processimages.main(proc_img_path, proc_img_url, all_set, save_path,
                           model_path, segments, False, threshold_score, limit)
    else:
        print_help(ctx, None, value=True)
Exemple #3
0
def image(ctx, proc_img_path, proc_img_url, segments, all_set, model_path,
          threshold_score):
    ''' Show processed image '''
    from maskrcnn_modanet import processimages

    if (not segments or
        (segments and not all_set)) and ((1 if proc_img_path else 0) +
                                         (1 if proc_img_url else 0) +
                                         (1 if all_set else 0)) == 1:
        processimages.main(proc_img_path, proc_img_url, all_set, None,
                           model_path, segments, False, threshold_score)
    else:
        print_help(ctx, None, value=True)
Exemple #4
0
def annotations(ctx, proc_img_path, proc_img_url, save_path, model_path,
                threshold_score):
    ''' Save processed image annotations '''
    segments = True
    all_set = False
    if (not segments or
        (segments and not all_set)) and ((1 if proc_img_path else 0) +
                                         (1 if proc_img_url else 0) +
                                         (1 if all_set else 0)) == 1:
        processimages.main(proc_img_path, proc_img_url, False, save_path,
                           model_path, segments, True, threshold_score)
    else:
        print_help(ctx, None, value=True)
Exemple #5
0
def annotations(ctx, proc_img_path, proc_img_url, model_path, threshold_score):
    ''' Show processed image annotations '''
    from maskrcnn_modanet import processimages
    segments = True
    all_set = False
    if (not segments or
        (segments and not all_set)) and ((1 if proc_img_path else 0) +
                                         (1 if proc_img_url else 0) +
                                         (1 if all_set else 0)) == 1:
        print(
            processimages.main(
                proc_img_path, proc_img_url, False, None, model_path, segments,
                True, threshold_score))  #function returns the annotations
    else:
        print_help(ctx, None, value=True)
def instagramImpl(profile, limit=None, offset=0, process_images=True, profile_stats=True, choice=None, restore_result=False):      
	from maskrcnn_modanet.instagram_impl import InstaloaderURL
	import matplotlib.pyplot as plt
	import cv2

	import time

	import json
	import codecs
	import os

	import numpy as np

	from maskrcnn_modanet.cli import validators

	from PIL import Image



	with open(os.path.expanduser('~')+ '/.maskrcnn-modanet/' + 'savedvars.json') as f:
		savedvars = json.load(f)
	path = savedvars['datapath']

	img_path = path + "datasets/coco/images/"
	ann_path = path + "datasets/coco/annotations/"
	snp_path = path + "results/snapshots"


	timestr = time.strftime("%Y%m%d-%H%M%S")

	profile_path = path + 'results/instagram/'+ profile + '/'

	save_images_path = profile_path + 'images/'

	save_segments_path = profile_path + 'segments/'

	log_path = profile_path + timestr + '.txt'


	from instaloader import (InstaloaderException, InvalidArgumentException, Post, Profile, ProfileNotExistsException,
			   StoryItem, __version__, load_structure_from_file, TwoFactorAuthRequiredException,
			   BadCredentialsException)
	

	if not restore_result:

		instaloader = InstaloaderURL(dirname_pattern=path+'/results/instagram/{target}',download_pictures=True, download_videos=False, download_video_thumbnails=False,
								 download_geotags=False,
								 download_comments=False, save_metadata=False,
							)

		target = profile

		profile = instaloader.check_profile_id(target)

		log_file = open(log_path, 'w+')

		
		if os.path.exists(save_images_path):
			for the_file in os.listdir(save_images_path):
				file_path = os.path.join(save_images_path, the_file)
				try:
					if os.path.isfile(file_path):
						os.unlink(file_path)
					#elif os.path.isdir(file_path): shutil.rmtree(file_path)
				except Exception as e:
					print(e)

		save_images_path = validators.check_if_folder_exists(None, None, save_images_path)

		print(profile)

		url_pics = instaloader.profile_posts_urls(profile, limit=limit, offset=offset)

		# print(url_pics)

		pics = []

		# for url_pic in url_pics:


		if process_images and not profile_stats:
			from maskrcnn_modanet.processimages import loadModel, main

			model, labels_to_names = loadModel(model_type='default')
		elif process_images and profile_stats:
			from maskrcnn_modanet.processimages import loadModel

			model, labels_to_names = loadModel(model_type='coco')

			print('Now looking for images with only one person in the image..')

			
		
		from keras_retinanet.utils.image import read_image_bgr
		import requests
		from io import BytesIO

		url_pics_person = []

		pic_index = 0
		for url_pic in url_pics:
			if not process_images:
				viewImageFromURL(url_pic)
			elif process_images and not profile_stats:
				# image, draw = getImageFromURL(url_pic, draw=True)

				# img_anns = apply_mask(model, image, threshold_score=0.5)

				# for img_ann in img_anns:
				# 	if img_ann['category'] == 'person':


				main(proc_img_path=None, proc_img_url=url_pic, all_set=False, save_images_path=None, model_path=None, 
					segments=False, annotations=False, threshold_score=0.5, limit=None, model=model, labels_to_names=labels_to_names)
			elif process_images and profile_stats:
				from maskrcnn_modanet.processimages import apply_mask

				print(pic_index, end=' ')
				print(pic_index, end=' ', file=log_file)
				print(url_pic, file=log_file)

				try:
					image, draw = getImageFromURL(url_pic, draw=True)
				except Exception:
					print('Image ', pic_index, 'failed to download. Url tried below:\n' + url_pic + '\n\n Continuing to next image..')
					print('Image ', pic_index, 'failed to download. Url tried below:\n' + url_pic + '\n\n Continuing to next image..', file=log_file)
					continue

				image_area = len(image) * len(image[0])

				pic_index += 1

				img_anns = apply_mask(model, image, draw=draw, labels_to_names=labels_to_names, image_segments=False)

				one_person = 0
				for img_ann in img_anns:
					if img_ann['category'] == 'person' and img_ann['bbox'][2] * img_ann['bbox'][3] >= 0.1 * image_area:
						one_person += 1
						print(one_person, 'person that covers ', round(img_ann['bbox'][2] * img_ann['bbox'][3] / image_area * 100),'%  of the image found in this photo', file=log_file)
					if one_person > 1:
						# we only select images with only one person that covers an area greater than 10% of the image
						print('Too many people found in this photo', file=log_file)
						one_person = 0
						break

				if one_person:
					# # show the image
					# plt.figure(figsize=(15, 15))
					# plt.axis('off')
					# plt.imshow(draw)
					# plt.show()

					# add the pic to the new urls
					url_pics_person.append([pic_index, url_pic])

		if process_images and profile_stats:
			print('We\'ve now selected the images that are probably the ones with only the person who owns this account.')
			if not choice:
				choice = ''
			while choice not in ['i', 's']:
				choice = input('Do you want to see the images processed or to see some stats? Type \'i\' for image, \'s\' for stats: ')

			# now let's switch to ModaNet and look into the image

			model, labels_to_names = loadModel(model_type='default')

			for label_index in labels_to_names:
				segment_label_path = save_segments_path + labels_to_names[label_index] + '/'

				# remove all previous segments saved

				if os.path.exists(segment_label_path):
					for the_file in os.listdir(segment_label_path):
						file_path = os.path.join(segment_label_path, the_file)
						try:
							if os.path.isfile(file_path):
								os.unlink(file_path)
							#elif os.path.isdir(file_path): shutil.rmtree(file_path)
						except Exception as e:
							print(e)

				segment_label_path = validators.check_if_folder_exists(None, None, segment_label_path)

			labels_images = {}

			for pic_index, url_pic in url_pics_person:
				image, draw = getImageFromURL(url_pic, draw=True)

				print(pic_index, end=' ')
				print(pic_index, end=' ', file=log_file)
				print(url_pic, file=log_file)

				

				if choice == 'i':
					img_anns = apply_mask(model, image, draw=draw, labels_to_names=labels_to_names, image_segments=False)
					# show the image
					plt.figure(figsize=(15, 15), num=str(pic_index))
					plt.axis('off')
					plt.imshow(draw)

					plt.show()
				elif choice == 's':
					img_anns = apply_mask(model, image, draw=draw, labels_to_names=labels_to_names, image_segments=True)

					# save the images for easy retrieval
					# plt.figure(num=str(pic_index), dpi=400)
					# plt.axis('off')
					# plt.imshow(draw)
					# plt.savefig(save_images_path + str(pic_index) + '.png')
					# plt.close()
					processed_image = Image.fromarray(draw, 'RGB')
					processed_image.save(save_images_path + str(pic_index) + '.png')
					del processed_image

					# let's count
					labels_images[pic_index] = {}
					for label_index in labels_to_names:
						labels_images[pic_index][labels_to_names[label_index]] = []

					for img_ann in img_anns:
						save_segment_path = save_segments_path + img_ann['category'] + '/' + str(pic_index) + '_.png'
						segment_counter = 1
						while os.path.isfile(save_segment_path):
							save_segment_path = "_".join(save_segment_path.split("_")[:-1]) + "_" + str(segment_counter) + '.png'
							segment_counter += 1
						segment = Image.fromarray(img_ann.pop('segment'), 'RGB')
						segment.save(save_segment_path)
						del segment
						img_ann['segment'] = save_segment_path
						labels_images[pic_index][img_ann['category']].append(img_ann)

			if choice == 's':
				results = {
					'url_pics': url_pics,
					'url_pics_person': url_pics_person,
					'labels_to_names': labels_to_names
				}

				print('Saving annotations results for easy recovery.. Use -r option later')
				with open(profile_path + 'results.json', 'w') as outfile:
					json.dump(results, outfile)
				print('Now saving annotations..')
				with open(profile_path + 'labels_images.json', 'wb') as outfile:
					np.save(outfile, labels_images)


	elif restore_result:
		log_file = open(log_path, 'w+')
		
		print('Restoring results..')
		with open(profile_path + 'results.json') as f:
			results = json.load(f)
		print('Restoring annotations..')
		with open(profile_path + 'labels_images.json', 'rb') as f:
			labels_images = np.load(f, allow_pickle=True)[()]
			# credit goes to https://stackoverflow.com/questions/30811918/saving-dictionary-of-numpy-arrays

		url_pics = results['url_pics']
		url_pics_person = results['url_pics_person']
		labels_to_names = results['labels_to_names']

		print('We\'ve now recovered the images that are probably the ones with only the person who owns this account, processed to look for apparel and clothing items.')
		if not choice:
			choice = ''
		while choice not in ['i', 's']:
			choice = input('Do you want to see the images processed or to see some stats? Type \'i\' for image, \'s\' for stats: ')

	if process_images and profile_stats:
		if choice == 'i' and restore_result:
			for pic_index, url_pic in url_pics_person:
				image, draw = getImageFromFilePath(save_images_path + str(pic_index) + '.png', draw=True)
				plt.figure(figsize=(9, 9), num=str(pic_index))
				plt.axis('off')
				plt.imshow(draw)
				plt.show()



		elif choice == 's':
			print('I will now show you all the stats I can think of:')
			print('I will now show you all the stats I can think of:', file=log_file)
			print('Total images:', len(url_pics), 'Images with one person: ', len(url_pics_person))
			print('Total images:', len(url_pics), 'Images with one person: ', len(url_pics_person), file=log_file)
			print(round(len(url_pics_person)/len(url_pics)*100, 1), '%  of the images contain only one main subject (probably the account owner)')
			print(round(len(url_pics_person)/len(url_pics)*100, 1), '%  of the images contain only one main subject (probably the account owner)', file=log_file)

			# for label_index in labels_to_names:
			# 	label = label[label_index]
			# how many of each label
			len_labels = {}
			
			for label_index in labels_to_names:
				len_labels[labels_to_names[label_index]] = 0
				
			for pic_index in labels_images:
				for label in labels_images[pic_index]:
					len_labels[label] += len(labels_images[pic_index][label])
			sum_len_labels = sum(len_labels[i] for i in len_labels)
			print('There are ', sum_len_labels, ' total instances of labels')
			print('There are ', sum_len_labels, ' total instances of labels', file=log_file)
			print(f'Instances of labels per image: {sum_len_labels/len(url_pics_person):4.2f}')
			print(f'Instances of labels per image: {sum_len_labels/len(url_pics_person):4.2f}', file=log_file)
			for label in len_labels:

				perc_label = len_labels[label]/sum_len_labels
				avg_label = sum(len(labels_images[pic_index][label]) for pic_index in labels_images)/len(labels_images)
				print(f'Label: {label:15} Perc: {len_labels[label]/sum_len_labels:>6.1%} | Per Image: Avg: {len_labels[label]/len(labels_images):>4.2f} Max: {max(len(labels_images[pic_index][label]) for pic_index in labels_images)}')
				print(f'Label: {label:15} Perc: {len_labels[label]/sum_len_labels:>6.1%} | Per Image: Avg: {len_labels[label]/len(labels_images):>4.2f} Max: {max(len(labels_images[pic_index][label]) for pic_index in labels_images)}', file=log_file)

			label_again = True
			while label_again:
				label = ' '
				while label not in len_labels and label != '':
					label = input('Insert a label to see its instances!\nUseful if you want to see which shoes your favourite instagram user has.\nYou can see the labels above. Press enter to abort: ')
				if label == '':
					label_again = False
					break
				segments = [img_ann['segment'] for pic_index in labels_images
												for img_ann in labels_images[pic_index][label] ]
				if len(segments) > 0:
					print('There are ', len(segments), ' results. Tell me the start and the end, as if you were slicing a Python array')
					print('You can also find the results in the folder:\n' + "/".join(segments[0].split("/")[:-1]))
					from_i = input('Start: ')
					if from_i == '':
						from_i = None
					else:
						from_i = int(from_i)
					to_i = input('End: ')
					if to_i == '':
						to_i = None
					else:
						to_i = int(to_i)

					

					for segment_path in segments[from_i:to_i]:
						img = Image.open(segment_path)
						img.show(title=segment_path.split('/')[-1])
						del img
				else:
					print('There are no segments to show for this label.\n')
					
					# plt.figure(figsize=(5, 5), num=str(pic_index), dpi=400)
					# plt.axis('off')
					# plt.imshow(segment)
					# plt.show()




	print('You can find the logs as a txt file in:\n' + log_path)

	log_file.close()