def fetch_image_urls(path): data_dictionary = [] data_urls = [] tl_list = [] br_list = [] raw_data = read_json_file(path) for idx, data_dict in enumerate(raw_data): data_dict, img_tl_list, img_br_list = generate_tl_br_list( data_dict=data_dict, data_urls=data_urls) data_dictionary.append(data_dict) tl_list.append(img_tl_list) br_list.append(img_br_list) objects = [] for i in range(len(data_dict.get('annotation'))): objects.append(data_dict.get('annotation')[i].get('label')[0]) write_xml(folder='./dataset', img_path='./dataset/image{}.jpg'.format(str(idx)), img_name='image{}.jpg'.format(str(idx)), objects=objects, tl=tl_list[idx], br=br_list[idx], savedir='./annotation') print("[INFO]: Xml files has been generated!") if os.path.getsize('./image_urls.txt') == 0: write_image_urls_to_file(data_urls) return data_dictionary, data_urls, tl_list, br_list
def onkeypress(event): global object_list global tl_list global br_list global img if event.key == 'r': tl_list = [] br_list = [] object_list = [] print("Object List cleared") # if event.key == 'r': # print(object_list) # write_xml(image_folder, img, object_list, tl_list, br_list, savedir) # tl_list = [] # br_list = [] # object_list = [] # img = None # keyboard = Controller() # keyboard.press('q') # keyboard.release('q') if event.key == 't': print(object_list) write_xml(image_folder, img, object_list, tl_list, br_list, savedir, sys.argv[1]) img = None keyboard = Controller() keyboard.press('q') keyboard.release('q')
def onekeypress(event): global br_list global tl_list global object_list global img if event.key == 'q': write_xml(image_folder, img, object_list, tl_list, br_list, savedir) br_list = [] tl_list = [] object_list = [] img = None plt.close()
def onkeypress(event): global object_list global tl_list global br_list global img if event.key == 'q': print(object_list) write_xml(image_folder, img, object_list, tl_list, br_list, savedir) tl_list = [] br_list = [] object_list = [] img = None
def onkeypress(event): global object_list global tl_list global br_list global img if event.key == 'q': write_xml(image_folder, img, object_list, tl_list, br_list, savedir) # 변수 초기화 & 이미지 닫기 tl_list = [] br_list = [] object_list = [] img = None
def onkeypress(event): global object_list global tl_list global br_list global img if event.key == "q": print(object_list) write_xml(IMAGE_FOLDER, img, object_list, tl_list, br_list, SAVE_DIR) tl_list = [] br_list = [] object_list = [] img = None plt.close()
def onkeypress(event): global object_list global top_left_list global bottom_right_list global img if event.key == 'q': print(top_left_list, bottom_right_list) write_xml(image_folder, path, object_list, top_left_list, bottom_right_list, savedir) top_left_list = [] bottom_right_list = [] object_list = [] plt.close()
def makeAnnos(lego, top_l_arr, name, width, height, n): out_top_l = [] out_bot_r = [] for l in range(len(lego)): xmin, ymin = getEdges(lego[l]) top_l = top_l_arr[l] stop_l = top_l top_l = (top_l[0] + xmin[0] - 5, top_l[1] + ymin[0] - 5) bot_r = (stop_l[0] + xmin[1] + 5, stop_l[1] + ymin[1] + 5) out_top_l.append(top_l) out_bot_r.append(bot_r) write_xml("Images", [width, height, 3], n, out_top_l, out_bot_r, savedir_anno, name)
def on_key_press(event): global object_list global top_left_list global bottom_right_list global img if event.key == 'q': write_xml(image_folder, img, object_list, top_left_list, bottom_right_list, save_directory) # print(top_left_list, bottom_right_list) top_left_list = [] bottom_right_list = [] object_list = [] img = None plt.close()
def onkeypress(event): global object_list global tl_list global br_list global img if event.key == "q": write_xml(data_folder, img, object_list, tl_list, br_list, savedir, "jpg") object_list = [] tl_list = [] br_list = [] img = None plt.close()
def onkeypress(event): # TODO Implement multi object declaration (Change savedir) global object_list global tl_list global br_list global img if event.key == 'q': print(object_list) write_xml('Tes_Model/images', img, object_list, tl_list, br_list, savedir) tl_list = [] br_list = [] object_list = [] img = None plt.close()
def onkeypress(event): global object_list global tl_list global br_list global img global save global objType global isSaved if event.key == 'c': objType = objType + 1 if objType == 2: objType = 0 print("Current type:" + classes[int(objType)]) if event.key == 'b': save = True if event.key == 't': tl_list = [] br_list = [] object_list = [] print("Resetted") if event.key == 'q': print(tl_list, br_list, object_list) #write_txt_file(tl_list, br_list, object_list) if len(object_list) > 0: write_xml(image_folder, img, object_list, tl_list, br_list, savedir, 0) mirror(image_folder, savedir, img, tl_list, br_list, object_list) scale(image_folder, savedir, img, tl_list, br_list, object_list) translation(image_folder, savedir, img, tl_list, br_list, object_list) print("Skrev til xml") tl_list = [] br_list = [] object_list = [] img = None plt.close() if event.key == 'm': plt.close() #if not isSaved: #write_txt_file(tl_list, br_list, object_list) global running global annotation_file #annotation_file.close() running = False
def onkeypress(event): global object_list global tl_list global br_list global img if event.key == 'a': #press p if it's a person object_list.append(obj1) write_xml(image_folder, img, object_list,tl_list,br_list,savedir) print(tl_list,br_list,object_list) #write_xml(image_folder, img, object_list, tl_list, br_list, savedir) tl_list = [] br_list = [] object_list = [] img = None plt.close() '''
def onkeypress(event): global object_list global tl_list global br_list global img,obj if event.key == 'q': print(object_list) write_xml(image_folder, img, object_list, tl_list, br_list, savedir) tl_list = [] br_list = [] object_list = [] img = None plt.close() if event.key in ["1","2","3","4","5","6","7","8","9"]: obj = objlist[int(event.key )-1] print(event.key,obj)
def translation(image_src, xml_path, image, tl, br, obj): image_cv = cv2.imread(image.path) tl_translated = [] br_translated = [] height, width, depth = image_cv.shape #translation factors factor = 0.2 tx = random.randint(-int(width * factor), int(width * factor)) ty = random.randint(-int(height * factor), int(height * factor)) #Translation matrix M = np.float32([[1, 0, tx], [0, 1, ty]]) translated_image = cv2.warpAffine(image_cv, M, (width, height)) print("%f %f" % (tx, ty)) for i in range(0, len(tl)): if ((tx + tl[i][0]) > width and (tx + br[i][0]) > width) or ((tx + tl[i][0]) < 0 and (tx + br[i][0]) < 0): print("bb er utenfor x-omraade") continue if ((ty + tl[i][1]) > height and (ty + br[i][1]) > height) or ((ty + tl[i][1]) < 0 and (ty + br[i][1]) < 0): print("bb er utenfor y-omraade") continue tl_translated.append( ((tx + tl[i][0] if (tx + tl[i][0]) > 0 else 0), (ty + tl[i][1] if (ty + tl[i][1] > 0) else 0))) br_translated.append( ((tx + br[i][0] if (tx + br[i][0]) < width else width), (ty + br[i][1] if (ty + br[i][1] < height) else height))) print("tl_translated", tl_translated) print("br_translated", br_translated) #cv2.rectangle(translated_image, tl_translated[i], br_translated[i], (255, 0, 0), 1) write_xml(image_src, image, obj, tl_translated, br_translated, xml_path, 3) cv2.imwrite(image_src + "/" + "t" + image.name, translated_image)
def onkeypress(event): global object_list global tl_list global br_list global img if event.key == 'q': write_xml(target_folder_gray, img, object_list, tl_list, br_list, savedir) print(f'[Final] \n{tl_list}\n{br_list}\n{object_list}\n') tl_list = [] br_list = [] img = None object_list = [] plt.close() if event.key == 'a': os.remove(img) print('[Deleted the image]\n') plt.close()
def mirror(image_src, xml_path, image, tl, br, obj): image_cv = cv2.imread(image.path) image_flipped = cv2.flip(image_cv, 1) tl_mirrored = [] br_mirrored = [] height, width, depth = image_cv.shape print(len(tl)) for i in range(0, len(tl)): tl_mirrored.append((width - br[i][0], tl[i][1])) br_mirrored.append((width - tl[i][0], br[i][1])) #cv2.rectangle(image_flipped, tl_mirrored[i], br_mirrored[i], (255, 0, 0), 1) cv2.imwrite(image_src + "/" + "m" + image.name, image_flipped) #Safeguard to prevent xml file writing with a non existent image if os.path.isfile(image_src + "/" + "m" + image.name): print("writing translated xml_file") write_xml(image_src, image, obj, tl_mirrored, br_mirrored, xml_path, 1)
def onkeypress(event): global object_list global tl_list global br_list global img if event.key == 'q': #Next Image print(object_list) if(tl_list and br_list)!=[]: write_xml(image_folder, img, object_list, tl_list, br_list, savedir) tl_list = [] br_list = [] object_list = [] img = None else: object_list = [] img = None if event.key=='w': #to clear any box drawn by mistake. tl_list = [] br_list = [] object_list = []
def on_key_press(event): global object_list global tl_list global br_list global img if event.key == "q": write_xml("imgData", img, object_list, tl_list, br_list, path + save_dir) os.rename(img.path, "../resources/imgData/" + img.name) tl_list = [] br_list = [] object_list = [] img = None plt.close() if event.key == "w": os.remove(img.path) tl_list = [] br_list = [] object_list = [] img = None plt.close()
def onkeypress(event): global object_list global tl_list global br_list global tl global br global img if event.key == 'q': print(object_list) tl.append(tl_list[-1]) br.append(br_list[-1]) write_xml(image_folder, img, object_list, tl, br, savedir) tl_list = [] br_list = [] object_list = [] tl = [] br = [] img = None if event.key == 'w': tl.append(tl_list[-1]) br.append(br_list[-1])
def scale(image_src, xml_path, image, tl, br, obj): image_cv = cv2.imread(image.path) tl_scaled = [] br_scaled = [] #Scaling factors fx = random.uniform(0.75, 1.25) fy = random.uniform(0.75, 1.25) scaled_img = cv2.resize(image_cv, None, fx=fx, fy=fy, interpolation=cv2.INTER_CUBIC) print("%f %f" % (fx, fy)) for i in range(0, len(tl)): tl_scaled.append((int(fx * tl[i][0]), int(fy * tl[i][1]))) br_scaled.append((int(fx * br[i][0]), int(fy * br[i][1]))) #cv2.rectangle(scaled_img, tl_scaled[i], br_scaled[i], (255, 0, 0), 1) write_xml(image_src, image, obj, tl_scaled, br_scaled, xml_path, 2) cv2.imwrite(image_src + "/" + "s" + image.name, scaled_img)
pt1 = (xmin, ymin) pt2 = (xmax, ymax) cv2.rectangle(image, pt1, pt2, (255, 255, 0), 3) loc = (xmin, ymin - 20) cv2.putText(image, pid, loc, cv2.FONT_HERSHEY_SIMPLEX, 1.5, (255, 0, 255), 6) #The frame is ready and already captured # cv2.namedWindow('video', cv2.WINDOW_NORMAL) # cv2.imshow('video', image) # if cv2.waitKey(1) & 0xFF == ord('q'): # break videowriter.write(image) bbox.append((xmin, ymin, xmax, ymax)) objects.append(str(pid)) # f = open(write_fname,'a') # txt = pid + ' ' + str(xmin) + ' ' + str(ymin) + ' ' + str(xmax) + \ # ' ' + str(ymax) + ' ' # f.write(txt) # f.write('\n') filename = 'frame_no_' + '{0:05}'.format(frame_no) + '.jpg' write_xml(folder, orig_image, filename, path, objects, bbox, savedir) print("--- %s seconds ---" % (time.time() - start_time)) cap.release() videowriter.release() #cv2.destroyAllWindows()
) #Arka plan üzerine koyulan resim için rastgele bir satır belirleniyor ancak bu sayı merkezi temsil ettiği için arkaplanın maksimum yüksekliğinden yerleştirilen resmin yüksekliğinin yarısı çıkartılıyor. rndCol = rd.randint(0, (Backgroundimage.shape[1] - Croppedimage.shape[1] )) #yukarıdaki işlem sütun için tekrarlanıyor. img1[ rndRow:rows + rndRow, rndCol:cols + rndCol] = dst #Kesilmiş resmin arkaplan üzerine koyulması tamamlandı. #cv2.imshow('res', img1) cv2.imwrite(dataset_folder_path + 'FinalImages\\{:06}.png'.format(i), img1) #resimler altı haneli bir formatta kaydediliyor. br_list.append( (int(rndRow + (Croppedimage.shape[0] / 2)), (int(rndCol + (Croppedimage.shape[1] / 2)))) ) #rastgele belirlenen merkez noktası üzerinden aşağı sağ(bottom right br) ve yukarı sol (top left tl) noktaları hesaplanıyor tl_list.append( (int(rndRow - (Croppedimage.shape[0] / 2)), (int(rndCol - (Croppedimage.shape[1] / 2)) ))) #hesaplanan bu noktalar xml oluştururken kullanılacak object_list.append( CropfileName.name ) #Klasör ismi aynı zaman içerisindeki resimlerin eğitim türü adı oluyor write_xml( image_folder, dataset_folder_path + 'FinalImages\\{:06}.png'.format(i), '{:06}.png'.format(i), object_list, tl_list, br_list, savedir ) #xml oluşturma komutu. Bu komut ile annotation dosyaları oluşuyor. tl_list.clear() #listeler sıfırlanıyor br_list.clear() object_list.clear() i = i + 1 #isimlendirmek için kullanılan sayaç bir artırılıyor.