def genDistractors(self): # generate distractor images for j in range(self.optionNum): plt.figure() for i in self.distractors: # do something and then draw the distractors. choice = random.choice(['flip', 'rotate', 'swap']) if choice == 'flip': i.flip(how=random.choice(['vert', 'hori'])) elif choice == 'rotate': i.rotate(theta=random.choice( [math.pi / 2, math.pi / 4, math.pi])) elif choice == 'swap': i.swap_polygons(random.choice(self.distractors)) i.drawPolygon() plt.axis('image') plt.axis('off') distractor_tmpPath = self.STATIC_ROOT + 'tmp/' + self.user_id + "_" + self.session_id + '_cut_question_' + str( self.questionCount) + '_dist_' + str(j) + '.png' plt.savefig(distractor_tmpPath) # crop the image img = cv2.imread(distractor_tmpPath, 0) img = cropImage(img) cv2.imwrite(distractor_tmpPath, img) # remove one quadrant from distractor full image and generate options img = cv2.imread(distractor_tmpPath, 0) quad, rest_img = splitQuad(img, self.quadrantNum) distractor_finalPath = self.STATIC_ROOT + 'result/' + self.user_id + "_" + self.session_id + '_cut_question_' + str( self.questionCount) + '_dist_' + str(j) + '.png' cv2.imwrite(distractor_finalPath, quad) # cv2.imwrite('./plot/quads/plotRest'+str(l)+'Dist'+str(j)+'.png',rest_img) # self.distractors_path.append(os.path.split(distractor_finalPath)[1]) self.distractors_path.append(distractor_finalPath) plt.close()
def generate_question_answer_pair(self): # generate question image plt.figure() # Make a random polygon (the outer most Polygon) A = Polygon() A.makeRandomCircumcircle() A.drawPolygon() # store distractor sequence self.distractor_sequence(A) plt.axis('image') plt.axis('off') question_tmpPath = self.STATIC_ROOT + 'tmp/' + self.user_id + "_" + self.session_id + '_cut_question_' + str( self.questionCount) + '.png' plt.savefig(question_tmpPath) # crop the question image img = cv2.imread(question_tmpPath, 0) img = cropImage(img) cv2.imwrite(question_tmpPath, img) # remove one quadrant from question and generate answer self.question_path = self.STATIC_ROOT + 'result/' + self.user_id + "_" + self.session_id + '_cut_question_' + str( self.questionCount) + '.png' self.answer_path = self.STATIC_ROOT + 'result/' + self.user_id + "_" + self.session_id + '_cut_answer_' + str( self.questionCount) + '.png' img = cv2.imread(question_tmpPath, 0) quad, rest_img = splitQuad(img, self.quadrantNum) cv2.imwrite(self.answer_path, quad) cv2.imwrite(self.question_path, rest_img) draw_boundary(self.quadrantNum, self.question_path) # os.system(' convert '+ self.question_path +' -bordercolor Black -border 8x8 '+ self.question_path) # os.system(' convert '+ self.answer_path +' -bordercolor Black -border 4x4 '+ self.answer_path) plt.close()
def generate_all_images(self): sides = [0, 0, 2, 4] XX = [0, 10, 15, 20] YY = [10, 15, 0, 20] polys = [] plt.figure() temp = None for i in range(len(sides)): size = (i + 1)**2 * 100 temp = Polygon(no_of_sides=sides[i], isRegular=False) temp.circumcircle = Circumcircle(size, XX[i], YY[i]) temp.makeShape() temp.drawPolygon() polys.append(temp) plt.axis('image') plt.axis('off') question_baseImgPath = self.STATIC_ROOT + 'tmp/' + self.user_id + "_" + self.session_id + '_fold_question_' + str( self.questionCount) + '_base.png' plt.savefig(question_baseImgPath, transparent=True) plt.close() # save a flipped version question_baseImgFlippedPath = self.STATIC_ROOT + 'tmp/' + self.user_id + "_" + self.session_id + '_fold_question_' + str( self.questionCount) + '_base_flipped.png' os.system('convert ' + question_baseImgPath + ' -flop ' + question_baseImgFlippedPath) one = IMG.open(question_baseImgPath).convert('RGBA') two = IMG.open(question_baseImgFlippedPath).convert('RGBA') base_img_merge_filepath = self.STATIC_ROOT + 'tmp/' + self.user_id + "_" + self.session_id + '_fold_question_' + str( self.questionCount) + '_base_merge.png' IMG.alpha_composite(one, two).save(base_img_merge_filepath) img = cv2.imread(base_img_merge_filepath, 0) height, width = img.shape # gives transparent image self.question_path = self.STATIC_ROOT + 'result/' + self.user_id + "_" + self.session_id + '_fold_question_' + str( self.questionCount) + '.png' self.answer_path = self.STATIC_ROOT + 'result/' + self.user_id + "_" + self.session_id + '_fold_answer_' + str( self.questionCount) + '.png' os.system( 'convert ' + question_baseImgPath + ' -strokewidth 1 -fill none -stroke black -draw \"stroke-dasharray 5 3 line ' + str(width / 2) + ',0 ' + str(width / 2) + ',' + str(height) + '\" ' + self.question_path) os.system( 'convert ' + base_img_merge_filepath + ' -strokewidth 1 -fill none -stroke black -draw \"stroke-dasharray 5 3 line ' + str(width / 2) + ',0 ' + str(width / 2) + ',' + str(height) + '\" ' + base_img_merge_filepath) os.system('convert ' + base_img_merge_filepath + ' -fill white -draw "rectangle 0,0 ' + str(width / 2) + ',' + str(height) + '" ' + self.answer_path) # adds white bg os.system('convert -flatten ' + self.question_path + ' ' + self.question_path) # question os.system('convert -flatten ' + base_img_merge_filepath + ' ' + base_img_merge_filepath) # intermediate output os.system('convert -flatten ' + self.answer_path + ' ' + self.answer_path) # answer # distractors for j in range(3): dist_base_img = self.STATIC_ROOT + 'tmp/' + self.user_id + "_" + self.session_id + '_fold_question_' + str( self.questionCount) + '_dist_' + str(j) + 'base.png' dist_base_flipped_img = self.STATIC_ROOT + 'tmp/' + self.user_id + "_" + self.session_id + '_fold_question_' + str( self.questionCount) + '_dist_' + str(j) + 'base_flipped.png' dist_base_merge_img = self.STATIC_ROOT + 'tmp/' + self.user_id + "_" + self.session_id + '_fold_question_' + str( self.questionCount) + '_dist_' + str(j) + 'base_merge.png' plt.figure() # additional transformation for temp in polys: if random.random() <= 0.5: temp.flip(how=random.choice(['vert', 'hori'])) else: temp.rotate( random.choice([-1, +1]) * random.choice([math.pi / 4, math.pi / 2])) temp.drawPolygon() plt.axis('image') plt.axis('off') # same base image plt.savefig(dist_base_img, transparent=True) plt.close() # save a flipped version os.system('convert ' + dist_base_img + ' -flop ' + dist_base_flipped_img) one = IMG.open(dist_base_img).convert('RGBA') two = IMG.open(dist_base_flipped_img).convert('RGBA') IMG.alpha_composite(one, two).save(dist_base_merge_img) img = cv2.imread(dist_base_merge_img, 0) height, width = img.shape distractor_final_path = self.STATIC_ROOT + 'result/' + self.user_id + "_" + self.session_id + '_fold_question_' + str( self.questionCount) + '_dist_' + str(j) + '.png' os.system( 'convert ' + dist_base_img + ' -strokewidth 1 -fill none -stroke black -draw \"stroke-dasharray 5 3 line ' + str(width / 2) + ',0 ' + str(width / 2) + ',' + str(height) + '\" ' + dist_base_img) os.system( 'convert ' + dist_base_merge_img + ' -strokewidth 1 -fill none -stroke black -draw \"stroke-dasharray 5 3 line ' + str(width / 2) + ',0 ' + str(width / 2) + ',' + str(height) + '\" ' + dist_base_merge_img) os.system('convert ' + dist_base_merge_img + ' -fill white -draw "rectangle 0,0 ' + str(width / 2) + ',' + str(height) + '" ' + distractor_final_path) os.system('convert -flatten ' + dist_base_img + ' ' + dist_base_img) # base distractor os.system('convert -flatten ' + dist_base_merge_img + ' ' + dist_base_merge_img) # intermediate distractor output os.system( 'convert -flatten ' + distractor_final_path + ' ' + distractor_final_path) # final transformed distractor - folded self.distractors_path.append(distractor_final_path)
from src.Polygons.Polygons import Polygon, plt, rndangle, Circumcircle from src.Polygons.utils import cropImage, splitQuad import math, copy import random import cv2 import string import os import PIL.Image as IMG import matplotlib.path as mpath import matplotlib.patches as mpatches ''' sides = [0,0,2,4] XX = [0,10,15,20] YY = [10,15,0,20] polys = [] plt.figure() temp = None for i in range(len(sides)): # size = max(random.random()+0.5,0.3+i)* random.choice([70,100]) size = (i+1)**2 * 100 # print size temp = Polygon(no_of_sides=sides[i],isRegular=False) temp.circumcircle = Circumcircle(size,XX[i],YY[i]) temp.makeShape() temp.drawPolygon() polys.append(temp)
def generate_all_images(self): for i in range(1, self.TOTAL_FIG + 4): plt.figure() A = Polygon(no_of_sides=3 + (self.XX + i) % self.SIDE_REPEAT_FREQ, isRegular=False, hatch=self.two_hatches[(self.XX + i) % self.HATCH_REPEAT_FREQ]) A.makeRandomCircumcircle() A.drawPolygon() plt.axis('image') plt.axis('off') # part of the question if i < self.TOTAL_FIG: question_tmpPath = self.STATIC_ROOT + 'tmp/' + self.user_id + "_" + self.session_id + '_series_question_' + str( self.questionCount) + '_part_' + str(i) + '.png' plt.savefig(question_tmpPath) plt.close() img = cv2.imread(question_tmpPath, 0) img = cropImage(img) cv2.imwrite(question_tmpPath, img) os.system(' convert ' + question_tmpPath + ' -bordercolor Black -border 4x4 ' + question_tmpPath) # answer elif i == self.TOTAL_FIG: self.answer_path = self.STATIC_ROOT + 'result/' + self.user_id + "_" + self.session_id + '_series_answer_' + str( self.questionCount) + '.png' plt.savefig(self.answer_path) plt.close() img = cv2.imread(self.answer_path, 0) img = cropImage(img) cv2.imwrite(self.answer_path, img) os.system(' convert ' + self.answer_path + ' -bordercolor Black -border 4x4 ' + self.answer_path) # distractors else: distractor_path = self.STATIC_ROOT + 'result/' + self.user_id + "_" + self.session_id + '_series_question_' + str( self.questionCount) + '_dist_' + str(i - self.TOTAL_FIG - 1) + '.png' plt.savefig(distractor_path) plt.close() img = cv2.imread(distractor_path, 0) img = cropImage(img) cv2.imwrite(distractor_path, img) os.system(' convert ' + distractor_path + ' -bordercolor Black -border 4x4 ' + distractor_path) self.distractors_path.append(distractor_path) # build question montage question_path = self.STATIC_ROOT + 'tmp/' + self.user_id + "_" + self.session_id + '_series_question_' + str( self.questionCount) + '_part_' self.question_path = self.STATIC_ROOT + 'result/' + self.user_id + "_" + self.session_id + '_series_question_' + str( self.questionCount) + '.png' os.system('montage -mode concatenate -tile ' + str(self.TOTAL_FIG - 1) + 'x1 ' + question_path + '[1-' + str(self.TOTAL_FIG - 1) + '].png ' + self.question_path)
# ID = str(time.time()) # print('montage -mode concatenate -tile '+str(TOTAL_FIG)+'x1 ./plot/series/[0-'+str(TOTAL_FIG-1)+'].png ./plot/series/'+ID+'.png') # os.system('montage -mode concatenate -tile '+str(TOTAL_FIG)+'x1 ./plot/series/[0-'+str(TOTAL_FIG-1)+'].png ./plot/series/'+ID+'.png') # # print('montage -mode concatenate -tile 4x1 ./plot/series/['+str(TOTAL_FIG-1)+'-'+str(TOTAL_FIG+2)+'].png ./plot/series/'+ID+'dist.png') # a = map(str,range(max(SIDE_REPEAT_FREQ, HATCH_REPEAT_FREQ))) # random.shuffle(a) # print(a, ','.join(a)) # filename = '' # for i in a: # filename+= ' ./plot/series/dist'+i+'.png' # os.system('montage -mode concatenate -tile '+str(max(SIDE_REPEAT_FREQ, HATCH_REPEAT_FREQ))+'x1 '+filename+' ./plot/series/'+ID+'dist.png') # ############################################################################# """ polys = [] TOTAL_FIG = 6 for i in range(TOTAL_FIG): plt.figure() A = Polygon(no_of_sides= 3+int(random.random()*10), isRegular=True, hatch=None) A.makeRandomCircumcircle() polys.append(A) A.drawPolygon() # plt.axis('off') # plt.axis('image') plt.figure() for i in range(len(polys)):
def generate_all_images(self): dist_root_seq_of_polygons = [] for l in range(self.lenSequence): # Cleans canvas starts a new figure plt.figure() # Make a random polygon (the outer most Polygon) A = Polygon() A.makeRandomCircumcircle() # This stores the polgons that make up an image. # We should store this and use it to create a distractor image seqs_of_polygons = [] for j in range(self.numPolygons): # any number of sides B = Polygon(no_of_sides=int(random.random() * 3) + 3) # same center and radius B.clone_circumcircle(A) # Center remains same as A but the radius can be anything less than A's raduis B.circumcircle.radius = 0.5 * A.circumcircle.radius # make the sides B.makeShape() # draw the polygon on the canvas B.drawPolygon() seqs_of_polygons.append(B) A.circumcircle.radius = B.circumcircle.radius plt.axis('image') plt.axis('off') question_base_path = self.STATIC_ROOT + 'tmp/' + self.user_id + "_" + self.session_id + '_grid_question_' + str( self.questionCount) + '_' + str(l) + '_0.png' plt.savefig(question_base_path) plt.close() img = cv2.imread(question_base_path, 0) img = cropImage(img) cv2.imwrite(question_base_path, img) img = IMG.open(question_base_path) img.resize((300, 300), IMG.ANTIALIAS).save(question_base_path, quality=90) for level in [1, 2]: plt.figure() # transformations for each embedded polygon # TODO: shuffle order and rules to generate distractors for i in range(len(seqs_of_polygons)): if i == 0: seqs_of_polygons[i].rotate(math.pi * 0.5) # 90 degrees elif i == 1: seqs_of_polygons[i].flip('vert') elif i == 2: seqs_of_polygons[i].rotate(math.pi * 0.75) # 270 degrees seqs_of_polygons[i].drawPolygon() if l == 2 and level == 1: dist_root_seq_of_polygons = copy.deepcopy(seqs_of_polygons) plt.axis('image') plt.axis('off') question_transform_path = self.STATIC_ROOT + 'tmp/' + self.user_id + "_" + self.session_id + '_grid_question_' + str( self.questionCount) + '_' + str(l) + '_' + str( level) + '.png' plt.savefig(question_transform_path) plt.close() img = cv2.imread(question_transform_path, 0) img = cropImage(img) cv2.imwrite(question_transform_path, img) img = IMG.open(question_transform_path) img.resize((300, 300), IMG.ANTIALIAS).save(question_transform_path, quality=90) level_montage_path = self.STATIC_ROOT + 'tmp/' + self.user_id + "_" + self.session_id + '_grid_question_' + str( self.questionCount) + '_level_' + str(l) + '_final.png' if l == 2: self.answer_path = self.STATIC_ROOT + 'result/' + self.user_id + "_" + self.session_id + '_grid_answer_' + str( self.questionCount) + '.png' os.system("cp " + self.STATIC_ROOT + 'tmp/' + self.user_id + "_" + self.session_id + '_grid_question_' + str(self.questionCount) + '_2_2.png ' + self.answer_path) os.system("montage -mode concatenate -border 2 " + self.STATIC_ROOT + "tmp/" + self.user_id + "_" + self.session_id + "_grid_question_" + str(self.questionCount) + "_" + str(l) + "_[0-1].png -tile 3x1 -geometry +1+1 " + level_montage_path) # generate distractors for dist in range(3): # create copy of level 2 part 1 question - from which answer is generated dist_seq_of_polygons = copy.deepcopy( dist_root_seq_of_polygons) plt.figure() if dist == 0: # order of transformations are shuffled for the distractors for i in range(len(dist_seq_of_polygons)): if i == 0: dist_seq_of_polygons[i].rotate( math.pi * 0.75) # 270 degrees elif i == 1: dist_seq_of_polygons[i].flip('hori') elif i == 2: dist_seq_of_polygons[i].rotate( math.pi * 0.5) # 90 degrees dist_seq_of_polygons[i].drawPolygon() if dist == 1: # order of transformations are shuffled for the distractors for i in range(len(dist_seq_of_polygons)): if i == 0: dist_seq_of_polygons[i].rotate( math.pi * 0.5) # 90 degrees elif i == 1: dist_seq_of_polygons[i].rotate( math.pi * 0.75) # 270 degrees elif i == 2: dist_seq_of_polygons[i].flip('hori') dist_seq_of_polygons[i].drawPolygon() if dist == 2: # order of transformations are shuffled for the distractors for i in range(len(dist_seq_of_polygons)): if i == 0: dist_seq_of_polygons[i].flip('vert') elif i == 1: dist_seq_of_polygons[i].rotate( math.pi * 0.75) # 270 degrees elif i == 2: dist_seq_of_polygons[i].rotate( math.pi * 0.5) # 90 degrees dist_seq_of_polygons[i].drawPolygon() plt.axis('image') plt.axis('off') distractor_path = self.STATIC_ROOT + 'result/' + self.user_id + "_" + self.session_id + '_grid_question_' + str( self.questionCount) + '_dist_' + str(dist) + '.png' plt.savefig(distractor_path) plt.close() img = cv2.imread(distractor_path, 0) img = cropImage(img) cv2.imwrite(distractor_path, img) img = IMG.open(distractor_path) img.resize((300, 300), IMG.ANTIALIAS).save(distractor_path, quality=90) self.distractors_path.append(distractor_path) else: os.system("montage -mode concatenate -border 2 " + self.STATIC_ROOT + "tmp/" + self.user_id + "_" + self.session_id + "_grid_question_" + str(self.questionCount) + "_" + str(l) + "_[0-2].png -tile 3x1 -geometry +1+1 " + level_montage_path) self.question_path = self.STATIC_ROOT + 'result/' + self.user_id + "_" + self.session_id + '_grid_question_' + str( self.questionCount) + '.png' os.system("montage -mode concatenate -border 2 " + self.STATIC_ROOT + "tmp/" + self.user_id + "_" + self.session_id + "_grid_question_" + str(self.questionCount) + "_level_[0-2]_final.png -tile 1x3 -geometry +1+1 " + self.question_path)
def generate_all_images(self): if self.logic_choice < 0.5: plt.figure() draw_grid() polys = [] XX = 0 rank = range(1, 10) random.shuffle(rank) for i in rank: temp = Polygon(no_of_sides=int(random.random() * 6), isRegular=False, hatch='random') draw_polygon_grid(temp, i) polys.append(temp) XX += 1 # question plt.axis('image') plt.axis('off') question_tmpPath = self.STATIC_ROOT + 'tmp/' + self.user_id + "_" + self.session_id + \ '_figureMatrix_question_' + \ str(self.questionCount)+'_part_' + str(0) + '.png' plt.savefig(question_tmpPath, dpi=150) img = cv2.imread(question_tmpPath, 0) img = cropImage(img) cv2.imwrite(question_tmpPath, img) plt.close() # generates remaining 4 parts for question for i in range(1, 9): plt.figure() draw_grid() # shift polygons by one position temp_circumcircle = polys[0].circumcircle for j in range(len(polys) - 1): polys[j].circumcircle = polys[j + 1].circumcircle polys[len(polys) - 1].circumcircle = temp_circumcircle for k in range(len(polys)): polys[k].gen_points() polys[k].drawPolygon() plt.axis('image') plt.axis('off') if i in [1, 2, 3, 4]: question_tmpPath = self.STATIC_ROOT + 'tmp/' + self.user_id + "_" + self.session_id + \ '_figureMatrix_question_' + \ str(self.questionCount)+'_part_' + str(i) + '.png' elif i == 5: question_tmpPath = self.STATIC_ROOT + 'result/' + self.user_id + "_" + \ self.session_id + '_figureMatrix_answer_' + \ str(self.questionCount) + '.png' self.answer_path = question_tmpPath else: question_tmpPath = self.STATIC_ROOT + 'result/' + self.user_id + "_" + self.session_id + \ '_figureMatrix_question_' + \ str(self.questionCount)+'_dist_' + str(i-6) + '.png' self.distractors_path.append(question_tmpPath) plt.savefig(question_tmpPath, dpi=150) img = cv2.imread(question_tmpPath, 0) img = cropImage(img) cv2.imwrite(question_tmpPath, img) plt.close() else: plt.figure() draw_grid() polys = [] XX = 0 rank = range(1, 10) random.shuffle(rank) hatches = ['-', '+', 'x', '\\', '*', 'o', 'O', '.', '/', '|'] random.shuffle(hatches) for i in rank: temp = Polygon(no_of_sides=int(random.random() * 6) + 3, isRegular=False, hatch=hatches[i % 10]) draw_polygon_grid(temp, i) polys.append(temp) XX += 1 # question plt.axis('image') plt.axis('off') question_tmpPath = self.STATIC_ROOT + 'tmp/' + self.user_id + "_" + self.session_id + \ '_figureMatrix_question_' + \ str(self.questionCount)+'_part_' + str(0) + '.png' plt.savefig(question_tmpPath, dpi=150) img = cv2.imread(question_tmpPath, 0) img = cropImage(img) cv2.imwrite(question_tmpPath, img) plt.close() # generates remaining 4 parts for question for i in range(1, 9): plt.figure() draw_grid() # shift polygons by one position temp_circumcircle = polys[0].circumcircle for j in range(len(polys) - 1): polys[j].circumcircle = polys[j + 1].circumcircle polys[len(polys) - 1].circumcircle = temp_circumcircle for k in range(len(polys)): polys[k].gen_points() polys[k].drawPolygon() plt.axis('image') plt.axis('off') if i in [1, 2, 3, 4]: question_tmpPath = self.STATIC_ROOT + 'tmp/' + self.user_id + "_" + self.session_id + \ '_figureMatrix_question_' + \ str(self.questionCount)+'_part_' + str(i) + '.png' elif i == 5: question_tmpPath = self.STATIC_ROOT + 'result/' + self.user_id + "_" + \ self.session_id + '_figureMatrix_answer_' + \ str(self.questionCount) + '.png' self.answer_path = question_tmpPath else: question_tmpPath = self.STATIC_ROOT + 'result/' + self.user_id + "_" + self.session_id + \ '_figureMatrix_question_' + \ str(self.questionCount)+'_dist_' + str(i-6) + '.png' self.distractors_path.append(question_tmpPath) plt.savefig(question_tmpPath, dpi=150) img = cv2.imread(question_tmpPath, 0) img = cropImage(img) cv2.imwrite(question_tmpPath, img) plt.close() self.question_path = self.STATIC_ROOT + 'result/' + self.user_id + "_" + \ self.session_id + '_figureMatrix_question_' + \ str(self.questionCount) + '.png' os.system('montage -mode concatenate -tile 5x1 -border 5 ' + self.STATIC_ROOT + 'tmp/' + self.user_id + "_" + self.session_id + '_figureMatrix_question_' + str(self.questionCount) + '_part_[0-4].png ' + self.question_path)