Skip to content

Instantly share code, notes, and snippets.

@BernardoGO BernardoGO/label.py
Created Jul 25, 2016

Embed
What would you like to do?
import tensorflow as tf
import sys
import cv2
from PIL import Image
import tensorflow as tf
import numpy as np
from random import randint
import os
import PIL
import cv2
from PIL import ImageFont, ImageDraw
from PIL import Image
from PIL import ImageDraw
from shapely.geometry import Polygon, box
from time import gmtime, strftime
import pickle
sizex, sizey = 1936, 1296
idx = 1
classes_in_image = {}
def heatmap(rect_pos1):
print("drawing heatmap")
image = Image.new("RGB", (sizex, sizey), "black" )
global classes_in_image
#rect_pos = class_scores
colors = get_spaced_colors(len(classes_in_image))
dr = ImageDraw.Draw(image , 'RGBA')
font = ImageFont.truetype("sans-serif.ttf", 18)
for ie in range(len(rect_pos1)):
eft = classes_in_image[rect_pos1[ie][4]]
xxf = eft[6]-1
#print(len(colors))
#print(xxf)
color = colors[xxf]
#print(color)
#print(classes_in_image[rect_pos[ie][4]][6])
#if rect_pos1[ie][2]-rect_pos1[ie][0] == x_adds_max:
# continue
#elif rect_pos1[ie][3]-rect_pos1[ie][1] == y_adds_max:
# continue
dr.rectangle(((rect_pos1[ie][1]+randint(0,10), rect_pos1[ie][0]+randint(0,10)),(rect_pos1[ie][3]+randint(0,10),rect_pos1[ie][2]+randint(0,10))), fill=(color[0], color[1], color[2], int(rect_pos1[ie][-1]*100)), outline = None)
posx = 20
posy = 20
for x in classes_in_image:
posy += 40
#color = colors[classes_in_image[x][6]]
#dr.text((posx,posy),str(x),(color[0], color[1], color[2]),font=font)
image.save("heatmap.png", quality=100)
def get_spaced_colors(n):
print("generating colors:" + str(n))
max_value = 16581375 #255**3
interval = int(max_value / n)
colors = [hex(I)[2:].zfill(6) for I in range(0, max_value, interval)]
return [(int(i[:2], 16), int(i[2:4], 16), int(i[4:], 16)) for i in colors]
#png_data = tf.placeholder(tf.string, shape=[])
#decoded_png = tf.image.decode_jpeg(png_data, channels=3)
def runDetection(image_data, class_scores, x, y, x_adds, y_adds, limit = 0.60, target = None):
global idx
global classes_in_image
global Session
global graph_def
#image_data = image_data
softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
#from numpy import array
#images = tf.image.decode_jpeg(array(image_data[0]), channels=3)
#image = tf.image.convert_image_dtype(images, dtype=tf.float32)
#print([x.name for x in sess.graph.get_operations() ])
predictions = sess.run(softmax_tensor, \
{'DecodeJpeg:0':image_data})
# Sort to show labels of first prediction in order of confidence
top_k = predictions[0].argsort()[-len(predictions[0]):][::-1]
#active segmentation
for node_id in top_k:#deleted
human_string = label_lines[node_id]
score = predictions[0][node_id]
if score > limit and str(human_string) != "bkg" and str(human_string) != "plate":
#print(score)
if human_string not in classes_in_image:
classes_in_image[human_string] = [y,x,y+y_adds,x+x_adds, human_string, score, idx]
idx += 1
if classes_in_image[human_string][0] < score:
classes_in_image[human_string] = [y,x,y+y_adds,x+x_adds, human_string, score, idx]
if target is None or target == human_string:
class_scores.append([y,x,y+y_adds,x+x_adds, human_string, score])
##UNCOMMENT FOR DEBUGGING PURPOSES
#cv2.rectangle(image, (y, x), (y+y_adds, x+x_adds), (255,0,0), 2)
#print(type(image))
#cv2.putText(image, str(human_string) +": "+str(score),(int(y+5),int(x+20)), font, 0.5,(255,255,0),1)
break
#print ( classes)
#classes_in_image.extend(classes)
def joinROIS(class_scores):
print("joining rois")
foid = True
while foid:
breako = False
for ii in class_scores:
for xx in class_scores:
if xx == ii: continue
rectxx = box(xx[3],xx[2], xx[1], xx[0], True)
rectii = box(ii[3],ii[2], ii[1], ii[0], True)
#(not (ii[0] < xx[0]) or (ii[1] < xx[1]) or (ii[2] > xx[2]) or (ii[3] > xx[3])) and
if rectxx.intersects(rectii) and (ii[4] == xx[4]):
ii[0] = min(ii[0], xx[0])
ii[1] = min(ii[1], xx[1])
ii[2] = max(ii[2], xx[2])
ii[3] = max(ii[3], xx[3])
class_scores.remove(xx)
breako = True
break
if breako: break
if breako: continue
break
classes = {}
font = cv2.FONT_HERSHEY_SIMPLEX
diff = "/home/bernardo/Chicken-and-Waffles-2.jpg"
tstImg = "/home/bernardo/projects/100ND40X/DSC_1627.JPG"#"/home/bernardo/projects/hd/tf/supervised/Images/10feb04_static_cars_highland/img_0808.jpg"
#tstImg = "google-image(206).jpg"
label_lines = [line.rstrip() for line
in tf.gfile.GFile("/home/bernardo/projects/data1.txt")]#output_labels#home/bernardo/food
with tf.gfile.FastGFile("/home/bernardo/projects/data1.pb", 'rb') as f: #outputgraph
import time
start = time.time()
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
_ = tf.import_graph_def(graph_def, name='')
class_scores = []
class_scores_lv1 = []
with tf.Session() as sess:
img = Image.open(tstImg)
image = img.resize((sizex, sizey), Image.ANTIALIAS)#
width, height = image.size
print("runnning high level detection")
x_adds_max = 175
x_adds_min = 175
y_adds_max = 175
y_adds_min = 175
for x in range(0,width,100):
for y in range(0,height,100):
#different sizes sliding window
x_adds = x_adds_max
y_adds = y_adds_max
crop_img = image.crop((x, y, x+x_adds, y+y_adds))
rdata = crop_img.resize((75, 75), Image.ANTIALIAS)
#class_scores.append([y,x,y+y_adds,x+x_adds, human_string, score])
runDetection(rdata, class_scores_lv1, x, y, x_adds, y_adds, 0.20)
if len(class_scores_lv1) > 0 and class_scores_lv1[-1][-2] == "bkg":
y += 0
heatmap(class_scores_lv1)
joinROIS(class_scores_lv1)
x_adds2 = 100
y_adds2 = 100
print("runnning low level detection")
for lvl1 in class_scores_lv1:
for x in range(lvl1[1],lvl1[3],50):
for y in range(lvl1[0],lvl1[2],50):
crop_img = image.crop((x, y, x+x_adds2, y+y_adds2))
rdata = crop_img.resize((75, 75), Image.ANTIALIAS)
runDetection(rdata, class_scores, x, y, x_adds2, y_adds2, 0.61, lvl1[4])
#heatmap(class_scores)
joinROIS(class_scores)
print("drawing")
rect_pos = class_scores_lv1
colors = get_spaced_colors(len(classes_in_image))
dr = ImageDraw.Draw(image , 'RGBA')
font = ImageFont.truetype("sans-serif.ttf", 18)
for ie in range(len(rect_pos)):
eft = classes_in_image[rect_pos[ie][4]]
xxf = eft[6]-1
#print(len(colors))
#print(xxf)
color = colors[xxf]
#print(color)
#print(classes_in_image[rect_pos[ie][4]][6])
if rect_pos[ie][2]-rect_pos[ie][0] == x_adds_max:
continue
elif rect_pos[ie][3]-rect_pos[ie][1] == y_adds_max:
continue
dr.rectangle(((rect_pos[ie][1]+randint(0,10), rect_pos[ie][0]+randint(0,10)),(rect_pos[ie][3]+randint(0,10),rect_pos[ie][2]+randint(0,10))), fill=(color[0], color[1], color[2], 50), outline = (color[0], color[1], color[2]))
dr.text((int(rect_pos[ie][1]+5),int(rect_pos[ie][0]+(randint(0,80)))),str( rect_pos[ie][4]) + ": " + str(rect_pos[ie][5]) + "-" + str(rect_pos[ie][2]-rect_pos[ie][0]),(color[0], color[1], color[2]),font=font)
#cv2.rectangle(image, (rect_pos[ie][0]+randint(0,10), rect_pos[ie][1]+randint(0,10)), (rect_pos[ie][2]+randint(0,10),rect_pos[ie][3]+randint(0,10)), (color[0], color[1], color[2]), 2)
#cv2.putText(image,str( rect_pos[ie][4]) + ": " + str(rect_pos[ie][5]) + "-" + str(rect_pos[ie][2]-rect_pos[ie][0]),(int(rect_pos[ie][0]+5),int(rect_pos[ie][1]+(randint(0,80)))), font, 0.5,(color[0], color[1], color[2]),2)
#cv2.imshow("cropped", image)
#cv2.waitKey(0)
#cv2.imwrite("output.jpg", image)
# run your code
end = time.time()
elapsed = end - start
print("elapsed time: " + str(elapsed))
image.save("output.png", quality=100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.