def object_detection_on_an_image(): segment_image = instance_segmentation() segment_image.load_model( "/home/danil/Documents/object_recognition/src/mask_rcnn_coco.h5" ) # выборка по конкретному объекту # target_class = segment_image.select_target_classes(person=True) target_class = segment_image.select_target_classes() result = segment_image.segmentImage( # image_path="1city.jpg", image_path="2cars_people.jpeg", # рамки у объектов show_bboxes=True, # передаем тот объект, который хотим выделить # segment_target_classes=target_class, # вырезаем объект и сохраняем в отдельный файл extract_segmented_objects=True, save_extracted_objects=True, # выходное изображение output_image_name="output.jpg" ) # считает колличество объектов print(result[0]["scores"]) objects_count = len(result[0]["scores"]) print(f"Найдено объектов: {objects_count}")
def __init__(self, weights_path): ''' Constructor prepares segmentation model from pretrained weights :param weights_path: Path to file with pretrained weights for model ''' self.weights_path = weights_path self.model = instance_segmentation() self.model.load_model(self.weights_path)
def inst_seg(): capture = cv2.VideoCapture(0) segment_video = instance_segmentation( infer_speed="rapid") #setting_the_speed segment_video.load_model("mask_rcnn_coco.h5") #loading_the_datamodel segment_video.process_camera(capture, frames_per_second=10, show_bboxes=True, show_frames=True, frame_name="frame", output_video_name="inst_seg_out.mp4")
def Segment_an_Image(PathInput, Pathoutput): """ Color the pixel in an image where there are objects :param PathInput: string. Path input :param Pathoutput: string. Path output :return: Nothing """ segment_image = instance_segmentation() segment_image.load_model("mask_rcnn_coco.h5") segment_image.segmentImage(PathInput, output_image_name=Pathoutput)
def __init__(self, input_im, a_height, a_leglength): #actual height of the person measured manually self.a_height = a_height #actual leg length of the person(below belly button) measured manually self.a_leglength = a_leglength #ratio of headtoheight : 1:7.5 approx self.a_head = (1/7.5)*a_height #actual size of head #Detect object(person) from image instance_seg = instance_segmentation() #mask_rcnn_coco.h5 is the pre trained model for object detection instance_seg.load_model("mask_rcnn_coco.h5") self.segmask, output = instance_seg.segmentImage(input_im, show_bboxes= True, output_image_name = "final.jpg")
def post(self): if request.json: image = request.json['image'] image_string = base64.b64decode(image) image_data = BytesIO(image_string) img = Image.open(image_data) img.save(INPUT_IMAGE) instance_segment_image = instance_segmentation() instance_segment_image.load_model(INSTANCE_MODEL) instance_segment_image.segmentImage(INPUT_IMAGE, output_image_name=OUTPUT_IMAGE, show_bboxes=True) with open(OUTPUT_IMAGE, "rb") as img_file: my_string = base64.b64encode(img_file.read()) final_base64_image_string = my_string.decode('utf-8') return {"output_image": final_base64_image_string}
def do_instance(img_path): absolute_path = os.path.abspath("model/mask_rcnn_coco.h5") segment_image = instance_segmentation(infer_speed="average") segment_image.load_model(absolute_path) # Perform instance segmentation segmask, output = segment_image.segmentImage(img_path, show_bboxes=True) # Bounding boxes # segment_image.segmentImage("sample2.jpg", output_image_name="image2_bb_new.jpg", show_bboxes=True) # Return the segmented image return output # output = do_instance("../../image/load/meeting_room.jpg") # cv2.imwrite("../../image/save/meeting_room_new.jpg", output) # cv2.imwrite("../../model/mask_rcnn_coco.h5") # print(output.shape)
def Segment_a_Video(PathInput, Pathoutput, AddBox=False): """ This function takes a video and color the relevant object pixels :param PathInput: string. Input path :param Pathoutput: string. Output path :param AddBox: bool. If True then add boxes around the object :return: Nothing """ cap = cv2.VideoCapture(PathInput) fps = int(cap.get(cv2.CAP_PROP_FPS)) print('Pathoutput:' + str(Pathoutput)) segment_video = instance_segmentation(infer_speed="fast") segment_video.load_model("mask_rcnn_coco.h5") segment_video.process_video(PathInput, show_bboxes=AddBox, frames_per_second=fps, output_video_name=Pathoutput)
def object_detection_on_an_image(): segment_image = instance_segmentation() segment_image.load_model("path_to_model") target_class = segment_image.select_target_classes(person=True) result = segment_image.segmentImage( # image_path="1city.jpg", # image_path="2cars_people.jpeg", image_path="3silicon_valley.jpg", # show_bboxes=True, segment_target_classes=target_class, # extract_segmented_objects=True, # save_extracted_objects=True, # output_image_name="output.jpg" ) # print(result[0]["scores"]) objects_count = len(result[0]["scores"]) print(f"Найдено объектов: {objects_count}")
import pixellib from pixellib.instance import instance_segmentation cap = cv2.VideoCapture('videos/Abandoned_Mill_PA.mp4') segment_video = instance_segmentation() segment_video.load_model("corossion_model_gabor_rf") segment_video.process_video(cap, overlay = True, frames_per_second= 15, output_video_name="output_video.mp4")
def LoadInstanceSegmenter(): from pixellib.instance import instance_segmentation global Segmenter_Instance Segmenter_Instance = instance_segmentation() Segmenter_Instance.load_model(ModelsDir + "mask_rcnn_coco.h5")
# Main file for end-to-end OCR # Author: LI Yang # Institution: WeShare from PaddleOCR import PaddleOCR, draw_ocr from PIL import Image import pixellib from pixellib.instance import instance_segmentation import os import cv2 # Initialization ocr = PaddleOCR(use_angle_cls = True, lang = "ch") segment_image = instance_segmentation() segment_image.load_model("mask_rcnn_coco.h5") img_path = "../Img/" result_list = [] Lot_list = [] CAS_list = [] Serial_list = [] def draw_ocr_result(img, res, index): image = Image.open(img).convert('RGB') output_name = "./ocr_res/res" + str(index) + ".jpg" boxes = [line[0] for line in res] texts = [line[1][0] for line in res] scores = [line[1][1] for line in res] im_show = draw_ocr(image, boxes, texts, scores, font_path='PingFang.ttc')
[{"start_time": 1.73, "xs": 100, "ys": 323, "ws": 321, "hs": 323, "end_time": 2.33, "xe": 224, "ye": 222, "we": 222, "he": 229, "line_style": "solid"}, {"start_time": 1.73, "xs": 818, "ys": 111, "ws": 111, "hs": 111, "end_time": 4.87, "xe": 1111, "ye": 111, "we": 111, "he": 111, "line_style": "solid"}] (xs, ys) is the center of the box, ws and hs are weight and height of the box. ''' from pixellib.instance import instance_segmentation import cv2 import numpy as np import glob import os import json import copy instance_seg = instance_segmentation() instance_seg.load_model("mask_rcnn_coco.h5") path = "./video1/img/" # 文件夹目录 folders = os.listdir(path) # def extract_file_name(x): return int(x.split('/')[-1][:-4]) def Fun_3(p, y): a1, a2, a3, a4 = p return a1 * y**3 + a2 * y**2 + a3 * y + a4
import cv2 #Pixellib is important library for Instance and Semantic segmentation from pixellib.instance import instance_segmentation from pixellib.semantic import semantic_segmentation ins_seg = instance_segmentation() #For instance segementation sem_seg = semantic_segmentation() #For semantic segementation ins_seg.load_model('mask_rcnn_coco.h5') #sem_seg.model('mask_rcnn_coco.h5') cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() if ret: result = ins_seg.segmentFrame(frame, show_bboxes=True) #result = sem_seg.segmentFrameAsPascalvoc(frame) #result is tuple containing dict in 0 index and image in 1 index image = result[1] cv2.imshow("Instance", image) if cv2.waitKey(1) & 0xFF == ord("q"): break cap.release() cv2.destroyAllWindows()
def _load_segmentation_model(self): _instance_segmentation = instance_segmentation() _instance_segmentation.load_model(self._segmentation_model_path) return _instance_segmentation
def __init__(self): self.model = instance_segmentation() self.model.load_model('mask_rcnn_coco.h5')
# download the model #https://github.com/matterport/Mask_RCNN/releases # https://github.com/matterport/Mask_RCNN/releases/download/v2.0/mask_rcnn_coco.h5 # copy the model to c:\models import pixellib from pixellib.instance import instance_segmentation import cv2 segmentation_model = instance_segmentation() segmentation_model.load_model('c:/models/mask_rcnn_coco.h5') # we would like to detect a target object . # for example, lets target the Giraffe target_classes = segmentation_model.select_target_classes(person=True) # use the model # We will use the image : animals.jpg # we are going to detects the objects and extarct them segmask, output = segmentation_model.segmentImage('Object-Detection/PixelLib/animals.jpg', segment_target_classes=target_classes, extract_segmented_objects=True, save_extracted_objects = True, show_bboxes=True , output_image_name = "Object-Detection/PixelLib/animalsOut.jpg") #print (segmask) # this contains all the objects # lets see first the image res = segmask["extracted_objects"]
# Code adapted from Ayoola Lafenwa # https://github.com/ayoolaolafenwa/PixelLib import pixellib from pixellib.instance import instance_segmentation import cv2 as cv import socialdistance as sd #USER INPUT PARAMETERS camAngle = 60 scaleFactor = 1/40 # (dist/imgWidth) approx distance (metres) between bottom left & right corners of image imgWidth = 1000 minimumAllowedDistance = 1 # INITIALISE THE PIXELLIB PERSON DETECTOR segment_video = instance_segmentation(infer_speed = "rapid") segment_video.load_model("mask_rcnn_coco.h5") target_classes = segment_video.select_target_classes(person=True) # https://pixellib.readthedocs.io/en/latest/video_instance.html # https://towardsdatascience.com/video-segmentation-with-5-lines-of-code-87f798afb93 # OPEN WEBCAM VIDEO STREAM capture = cv.VideoCapture(0) # CHECK FRAME SIZE if capture.isOpened(): width = int(capture.get(cv.CAP_PROP_FRAME_WIDTH)) # float `width` height = int(capture.get(cv.CAP_PROP_FRAME_HEIGHT)) # float `height` print('width, height:', width, height) fps = capture.get(cv.CAP_PROP_FPS)
pos = nx.shell_layout(G) plt.figure() nx.draw(G, pos, edge_color='black', width=1, linewidths=1, node_size=500, node_color='seagreen', alpha=0.9, labels={node: node for node in G.nodes()}) for triple in triples: nx.draw_networkx_edge_labels(G, pos, edge_labels={(triple[0], triple[2]):triple[1]},font_color='k') plt.axis('off') plt.savefig("Graph.png", format="PNG") plt.show() instance_seg = instance_segmentation(infer_speed = "fast") instance_seg.load_model("mask_rcnn_coco.h5") url = "https://www.youtube.com/watch?v=LV8viSdfNl0" video = pafy.new(url) best = video.getbest(preftype="mp4") capture = cv2.VideoCapture(best.url) size = ( int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)), int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)) ) codec = cv2.VideoWriter_fourcc(*'DIVX') output = cv2.VideoWriter('videofile_masked.mp4', codec, 10.0, size)
#Definition of semantic_segmentation in pixellib Library segment_image = semantic_segmentation() #Es necesario los pesos de deeplabv3_xception_tf_dim_ordering_tf_kernels.h5 segment_image.load_pascalvoc_model("deeplabv3_xception_tf_dim_ordering_tf_kernels.h5") #Semantic Segmentation segment_image.segmentAsPascalvoc("test7_4.jpg", output_image_name = "person_segmentationN.jpg") #Semantic Segmentation with image Mask segment_image.segmentAsPascalvoc("test7_4.jpg", output_image_name = "person_segmentationN_Mask.jpg", overlay = True) print("Done") ##Instance Segmentation print("Instance Image Segmentation Process...") #Definition of Instance_segmentation in pixellib Library segment_image2 = instance_segmentation() #Es necesario los pesos de mask_rcnn_coco.h5 segment_image2.load_model("mask_rcnn_coco.h5") #Instance Segmentation segment_image2.segmentImage("test7_4.jpg", output_image_name = "person_segmentation_RCNN_N.jpg") ##Instance Segmentation with object boundary by category segment_image2.segmentImage("test7_4.jpg", output_image_name = "person_segmentation_RCNN_Masked_N.jpg", show_bboxes = True) print("Done")
scaleFactor = 1 / 40 # (dist/imgWidth) approx distance (metres) between bottom left & right corners of image imgWidth = 1000 minimumAllowedDistance = 1 imgOrig = cv.imread('sample2edit.jpg') # RESIZE IMAGE width = int(imgWidth) ratio = imgOrig.shape[1] / width height = int(imgOrig.shape[0] / ratio) dsize = (width, height) print(dsize) img = cv.resize(imgOrig, dsize) cv.imwrite("pixellibImgResize.jpg", img) segment_image = instance_segmentation( ) #rapid decrease accuracy for small people segment_image.load_model("mask_rcnn_coco.h5") target_classes = segment_image.select_target_classes(person=True) segmask, img = segment_image.segmentImage( "pixellibImgResize.jpg", segment_target_classes=target_classes) cv.imwrite("pixellibImgOutput.jpg", img) # REORGANISE SEGMASK['ROIS'] ARRAY bblines = [] for i in range(len(segmask['rois'])): a = [] a.append(segmask['rois'][i][1]) a.append(segmask['rois'][i][0]) a.append(segmask['rois'][i][3]) a.append(segmask['rois'][i][2])