def main(): st.title("Semantic Segmentation App for Images") st.text("Built with gluoncv and Streamlit") st.markdown( """### [Semantic Segmentation](https://towardsdatascience.com/semantic-segmentation-with-deep-learning-a-guide-and-code-e52fc8958823)\ ` `[PSPNet](https://towardsdatascience.com/review-pspnet-winner-in-ilsvrc-2016-semantic-segmentation-scene-parsing-e089e5df177d) \ ` `[[Paper]](https://arxiv.org/abs/1612.01105)\ ` `[![Open Source Love svg1](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)](https://github.com/Hardly-Human/Semantic-Segmentation-of-Images)\ ` `[![GitHub license](https://img.shields.io/github/license/Naereen/StrapDown.js.svg)](https://lbesson.mit-license.org/)""" ) image_file = st.file_uploader("Upload Image", type=['jpg', 'png', 'jpeg']) if image_file is None: st.warning( "Upload Image and Run Model (Use Image size <300 KB for faster inference)" ) if image_file is not None: image1 = Image.open(image_file) rgb_im = image1.convert('RGB') image2 = rgb_im.save("saved_image.jpg") image_path = "saved_image.jpg" st.image(image1, width=500, height=500) if st.button("Run Model"): st.warning("Loading Model..🤞") model = load_model('psp_resnet101_ade') img = image.imread(image_path) img = test_transform(img, ctx) st.success("Loaded Model Succesfully!!🤩👍") plot_image(model, img)
def process_frame(self, frame, frame_count, frameWidth, frameHeight): fcx = frame_width // 2 fcy = frame_height // 2 percent = 0.2 rightcx = int(fcx + percent * frame_width) leftcx = int(fcx - percent * frame_width) signal = Signal.NoPipe # cropped based on 240 frm = cv2.resize(frame, (224, 224)) img = mx.nd.array(frm, ctx=self.ctx) img = test_transform(img, ctx=self.ctx) output = self.model.predict(img) argx = mx.nd.topk(output, 1) predict = np.squeeze(argx).asnumpy() if (frame_count > 1): predict = self.etha * predict + (1 - self.etha) * self.previous self.previous = copy.copy(predict) ret, mask = cv2.threshold(predict.astype(float), .5, 255, cv2.THRESH_BINARY) if cv2.countNonZero(mask) > 0: mask = cv2.resize(mask, (frameWidth, frameHeight)) im2, contours, h = cv2.findContours(mask.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) if (len(contours) > 0): contourMask = max(contours, key=cv2.contourArea) # Centroid M = cv2.moments(contourMask) if M['m00'] != 0: cx = int(M['m10'] / M['m00']) cy = int(M['m01'] / M['m00']) self.kalman.correct( np.array([[np.float32(cx)], [np.float32(cy)]], np.float32)) cent = self.kalman.predict() if frame_count >= 25: cx = np.int(cent[0][0]) cy = np.int(cent[1][0]) signal = self.find_signal(cx, leftcx, rightcx) # Centroid cv2.circle(frame, (cx, cy), 0, (0, 255, 255), 5) # rectangle rect = cv2.minAreaRect(contourMask) box = cv2.boxPoints(rect) box = np.int0(box) cv2.drawContours(frame, [box], 0, (255, 255, 255), 1) return frame, cx, cy, signal.value return frame, 0, 0, Signal.NoPipe.value
def getSeg(self, np_img, saved_name=''): np_img = np.array(np_img) np_img = mx.ndarray.array(np_img) img = test_transform(np_img, self.ctx) # print(img) output = self.model.predict(img) predict = mx.nd.squeeze(mx.nd.argmax(output, 1)).asnumpy() predict = predict.astype(numpy.uint8) if saved_name != '': try: io.imsave(saved_name, predict, check_contrast=False) except Exception as e: print("Error in saving file in getSeg():", e, saved_name) return predict
import mxnet as mx from mxnet import image from mxnet.gluon.data.vision import transforms import gluoncv import cv2 # using cpu ctx = mx.cpu(0) filename= '71.jpeg' # load the image img = image.imread(filename) from matplotlib import pyplot as plt #plt.imshow(img.asnumpy()) #plt.show() from gluoncv.data.transforms.presets.segmentation import test_transform img = test_transform(img, ctx) # get pre-trained model model = gluoncv.model_zoo.get_model('fcn_resnet101_voc', pretrained=True) output = model.predict(img) predict = mx.nd.squeeze(mx.nd.argmax(output, 1)).asnumpy() from gluoncv.utils.viz import get_color_pallete import matplotlib.image as mpimg mask = get_color_pallete(predict, 'pascal_voc') mask.save('output.png')
def main(): start = timer() print('Processing Start time: %.1f' % (start)) print("current time", datetime.now()) gauth = GoogleAuth() gauth.LocalWebserverAuth() drive = GoogleDrive(gauth) # Auto-iterate through all files that matches this query file_list = drive.ListFile({'q': "'root' in parents"}).GetList() for file in file_list: # print('title: {}, id: {}'.format(file1['title'], file1['id'])) file_id = None if file['title'] == "semanticsegmentation": print('Folder Found') file_id = file['id'] break if file_id is not None: classes = ["wall","building;edifice","sky","floor;flooring","tree","ceiling","road;route","bed","windowpane;window","grass","cabinet","sidewalk;pavement","person;individual;someone;somebody;mortal;soul","earth;ground","door;double;door","table","mountain;mount","plant;flora;plant;life","curtain;drape;drapery;mantle;pall","chair","car;auto;automobile;machine;motorcar","water","painting;picture","sofa;couch;lounge","shelf","house","sea","mirror","rug;carpet;carpeting","field","armchair","seat","fence;fencing","desk","rock;stone","wardrobe;closet;press","lamp","bathtub;bathing;tub;bath;tub","railing;rail","cushion","base;pedestal;stand","box","column;pillar","signboard;sign","chest;of;drawers;chest;bureau;dresser","counter","sand","sink","skyscraper","fireplace;hearth;open;fireplace","refrigerator;icebox","grandstand;covered;stand","path","stairs;steps","runway","case;display;case;showcase;vitrine","pool;table;billiard;table;snooker;table","pillow","screen;door;screen","stairway;staircase","river","bridge;span","bookcase","blind;screen","coffee;table;cocktail;table","toilet;can;commode;crapper;pot;potty;stool;throne","flower","book","hill","bench","countertop","stove;kitchen;stove;range;kitchen;range;cooking;stove","palm;palm;tree","kitchen;island","computer;computing;machine;computing;device;data;processor;electronic;computer;information;processing;system","swivel;chair","boat","bar","arcade;machine","hovel;hut;hutch;shack;shanty","bus;autobus;coach;charabanc;double-decker;jitney;motorbus;motorcoach;omnibus;passenger;vehicle","towel","light;light;source","truck;motortruck","tower","chandelier;pendant;pendent","awning;sunshade;sunblind","streetlight;street;lamp","booth;cubicle;stall;kiosk","television;television;receiver;television;set;tv;tv;set;idiot;box;boob;tube;telly;goggle;box","airplane;aeroplane;plane","dirt;track","apparel;wearing;apparel;dress;clothes","pole","land;ground;soil","bannister;banister;balustrade;balusters;handrail","escalator;moving;staircase;moving;stairway","ottoman;pouf;pouffe;puff;hassock","bottle","buffet;counter;sideboard","poster;posting;placard;notice;bill;card","stage","van","ship","fountain","conveyer;belt;conveyor;belt;conveyer;conveyor;transporter","canopy","washer;automatic;washer;washing;machine","plaything;toy","swimming;pool;swimming;bath;natatorium","stool","barrel;cask","basket;handbasket","waterfall;falls","tent;collapsible;shelter","bag","minibike;motorbike","cradle","oven","ball","food;solid;food","step;stair","tank;storage;tank","trade;name;brand;name;brand;marque","microwave;microwave;oven","pot;flowerpot","animal;animate;being;beast;brute;creature;fauna","bicycle;bike;wheel;cycle","lake","dishwasher;dish;washer;dishwashing;machine","screen;silver;screen;projection;screen","blanket;cover","sculpture","hood;exhaust;hood","sconce","vase","traffic;light;traffic;signal;stoplight","tray","ashcan;trash;can;garbage;can;wastebin;ash;bin;ash-bin;ashbin;dustbin;trash;barrel;trash;bin","fan","pier;wharf;wharfage;dock","crt;screen","plate","monitor;monitoring;device","bulletin;board;notice;board","shower","radiator","glass;drinking;glass","clock","flag"] files = glob.glob(r'/Users/divyachandana/Documents/NJIT/work/summertasks/jun1-jun5/atlanta/*.jpg') print("Total Files",len(files)) columns = ['filename','class','total_pixel','individual_pixel','ratio','timestamp'] # ---------- drive code ----- with open('semantic_results_atlanta.csv','a') as csvfile: csvwriter = csv.writer(csvfile,lineterminator='\n') # csvwriter.writerow(columns) # i=0 for f in files: file_check_query = "select count(*) from {} where filename like '%{}%'".format('semantic_results_atlanta', os.path.basename(f)) # print(file_check_query) # i += 1 # print(i) count = dbms.get_count_result(file_check_query) # print(count) if count > 0: continue # print('resuming',f) try: img = image.imread(f) img = image.resize_short(img, 1024) # img = image.resize_short(img, 100) # print("filename: ", f) # ctx = mx.gpu(0) img = test_transform(img, ctx) # print("img: ", img) output = model.predict(img) # print("output: ", output) predict = mx.nd.squeeze(mx.nd.argmax(output, 1)).asnumpy() # print("predict: ", predict) mask = get_color_pallete(predict, 'ade20k') # predict.save('predict.png') # mmask = mpimg.imread('output.png') predict = predict.astype(numpy.uint8) convert_single_array = numpy.array(predict) unique_numbers = numpy.unique(convert_single_array) # print(unique_numbers) new_basename = os.path.basename(f).replace(".jpg", ".png") new_name = os.path.join('output/', new_basename) mask.save(new_name) # color_img = image.imread(new_name) # colors, counts = numpy.unique(color_img.reshape(-1, 3), return_counts=True, axis=0) total_pixel = numpy.sum(predict) d_file = drive.CreateFile({'parents': [{'id': file_id}], 'title': os.path.basename(new_name)}) d_file.SetContentFile(new_name) d_file.Upload() # print('Created file %s with mimeType %s' % (d_file['title'], d_file['mimeType'])) combile_all_csv_data = [] combine_sql_srting_format = [] for i in unique_numbers: individual_count = numpy.sum(predict == i) # print(individual_count) csv_data = [] csv_data.append(os.path.basename(f)) csv_data.append(classes[i]) csv_data.append(total_pixel) csv_data.append(individual_count) csv_data.append(round((individual_count/total_pixel),6)) time_stamp = datetime.now() csv_data.append(time_stamp) # csv_data = [filename,predict,colors,counts,total_pixel] # print(csv_data) combile_all_csv_data.append(csv_data) sql_srting = ["NULL" if val == None else "'"+str(val)+"'" for val in csv_data] sql_srting_format = ",".join([str(val) for val in sql_srting]) combine_sql_srting_format.append(sql_srting_format) csvwriter.writerows(combile_all_csv_data) dbms.insertmany_sqlite3('semantic_results_atlanta',','.join(columns),combine_sql_srting_format) os.remove(new_name) # if idx % 10 == 0: # print("Processed: ", idx) except Exception as e: print("Error in :", ' ' + f, e) continue print('Finished') end = timer() print('Processing time: %.1f' % (end - start))
def process_frame(self, frame, frame_count, frameWidth, frameHeight): # cropped based on 240 frm = cv2.resize(frame, (224, 224)) img = mx.nd.array(frm, ctx=self.ctx) img = test_transform(img, ctx=self.ctx) # img = img.astype('float32') output = self.model.predict(img) #argx = mx.nd.argmax(output, 1) argx = mx.nd.topk(output, 1) #argx = np.argmax(output, axis=1) # Wait to read actually waits for calculation # argx.wait_to_read() # or # np.squeeze(argx).wait_to_read() # tic = time.perf_counter() # tic = time.time() predict = np.squeeze(argx).asnumpy() # toc = time.time() # print("Proccesed in {}".format(toc-tic)) # predict = predict.astype(np.uint8) # current = cv2.fastNlMeansDenoising(predict,None,10,7,21) if (frame_count > 1): predict = self.etha * predict + (1 - self.etha) * self.previous # current = current.astype(np.uint8) self.previous = copy.copy(predict) ret, mask = cv2.threshold(predict.astype(float), .5, 255, cv2.THRESH_BINARY) if cv2.countNonZero(mask) > 0: mask = cv2.resize(mask, (frameWidth, frameHeight)) # contourMask = self.get_rough_contour(mask) im2, contours, h = cv2.findContours(mask.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) if (len(contours) > 0): contourMask = max(contours, key=cv2.contourArea) # Centroid M = cv2.moments(contourMask) if M['m00'] != 0: cx = int(M['m10'] / M['m00']) cy = int(M['m01'] / M['m00']) self.kalman.correct( np.array([[np.float32(cx)], [np.float32(cy)]], np.float32)) cent = self.kalman.predict() if frame_count >= 25: cx = np.int(cent[0][0]) cy = np.int(cent[1][0]) # Centroid cv2.circle(frame, (cx, cy), 0, (0, 255, 255), 5) # rectangle rect = cv2.minAreaRect(contourMask) box = cv2.boxPoints(rect) box = np.int0(box) cv2.drawContours(frame, [box], 0, (255, 255, 255), 1) # Line # rows,cols = mask.shape[:2] # [vx,vy,x,y] = cv2.fitLine(contourMask, cv2.DIST_L2,0,0.01,0.01) # lefty = int((-x*vy/vx) + y) # righty = int(((cols-x)*vy/vx)+y) # img = cv2.line(frame,(cols-1,righty),(0,lefty),(255,255,255),1) return frame, cx, cy return frame, 0, 0