def __init__(self, modelfile, shape=(300, 300, 3), num_classes=21, conf_thresh=0.6): self.input_shape = shape self.num_classes = num_classes self.conf_thresh = conf_thresh # モデル作成 model = SSD(shape, num_classes=num_classes) model.load_weights(modelfile) self.model = model # バウンディングボックス作成ユーティリティ self.bbox_util = BBoxUtility(self.num_classes)
class ssdKeras(): def __init__(self): #self.node_name = "ssd_keras" #rospy.init_node(self.node_name) self.class_names = ["background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"] self.num_classes = len(self.class_names) self.input_shape = (300,300,3) self.model = SSD(self.input_shape,num_classes=self.num_classes) self.model.load_weights('/home/abdulrahman/catkin_ws/src/victim_localization/resources/ssd_keras/weights_SSD300.hdf5') self.bbox_util = BBoxUtility(self.num_classes) self.conf_thresh = 0.7 self.model._make_predict_function() self.graph = tf.get_default_graph() self.detection_index=DL_msgs_boxes() # Create unique and somewhat visually distinguishable bright # colors for the different classes. self.class_colors = [] for i in range(0, self.num_classes): # This can probably be written in a more elegant manner hue = 255*i/self.num_classes col = np.zeros((1,1,3)).astype("uint8") col[0][0][0] = hue col[0][0][1] = 128 # Saturation col[0][0][2] = 255 # Value cvcol = cv2.cvtColor(col, cv2.COLOR_HSV2BGR) col = (int(cvcol[0][0][0]), int(cvcol[0][0][1]), int(cvcol[0][0][2])) self.class_colors.append(col) self.bridge = CvBridge() # Create the cv_bridge object self.image_sub = rospy.Subscriber("/image_raw_converted2", Image, self.detect_image,queue_size=1) # the appropriate callbacks self.box_coordinate_pub = rospy.Publisher("/ssd_detction/box", DL_msgs_boxes ,queue_size=5) # the appropriate callbacks def detect_image(self, ros_image): """ Runs the test on a video (or webcam) # Arguments conf_thresh: Threshold of confidence. Any boxes with lower confidence are not visualized. """ #### Use cv_bridge() to convert the ROS image to OpenCV format #### try: image_orig = self.bridge.imgmsg_to_cv2(ros_image, "bgr8") except CvBridgeError as e: print(e) ########## vidw = 1280.0 # change from cv2.cv.CV_CAP_PROP_FRAME_WIDTH vidh = 720.0 # change from cv2.cv.CV_CAP_PROP_FRAME_HEIGHT vidar = vidw/vidh #print(type(image_orig)) im_size = (self.input_shape[0], self.input_shape[1]) resized = cv2.resize(image_orig, im_size) rgb = cv2.cvtColor(resized, cv2.COLOR_BGR2RGB) # Reshape to original aspect ratio for later visualization # The resized version is used, to visualize what kind of resolution # the network has to work with. to_draw = cv2.resize(resized, (1280, 720)) # Use model to predict inputs = [image.img_to_array(rgb)] tmp_inp = np.array(inputs) x = preprocess_input(tmp_inp) start_time = time.time() #debuggin with self.graph.as_default(): y = self.model.predict(x) #print("--- %s seconds_for_one_image ---" % (time.time() - start_time)) # This line creates a new TensorFlow device every time. Is there a # way to avoid that? results = self.bbox_util.detection_out(y) if len(results) > 0 and len(results[0]) > 0: # Interpret output, only one frame is used det_label = results[0][:, 0] det_conf = results[0][:, 1] det_xmin = results[0][:, 2] det_ymin = results[0][:, 3] det_xmax = results[0][:, 4] det_ymax = results[0][:, 5] top_indices = [i for i, conf in enumerate(det_conf) if conf >= self.conf_thresh] top_conf = det_conf[top_indices] top_label_indices = det_label[top_indices].tolist() top_xmin = det_xmin[top_indices] top_ymin = det_ymin[top_indices] top_xmax = det_xmax[top_indices] top_ymax = det_ymax[top_indices] #initiaze the detection msgs box_msg = DL_msgs_box() box_msg.xmin=0 box_msg.ymin=0 box_msg.xmax=0 box_msg.ymax=0 box_msg.Class="Non" # 100 reflect a non-class value self.detection_index.boxes.append(box_msg) print (top_xmin) for i in range(top_conf.shape[0]): self.detection_index.boxes[:]=[] xmin = int(round(top_xmin[i] * to_draw.shape[1])) ymin = int(round(top_ymin[i] * to_draw.shape[0])) xmax = int(round(top_xmax[i] * to_draw.shape[1])) ymax = int(round(top_ymax[i] * to_draw.shape[0])) #include the corner to be published box_msg = DL_msgs_box() box_msg.xmin=xmin box_msg.ymin=ymin box_msg.xmax=xmax box_msg.ymax=ymax box_msg.Class=self.class_names[int(top_label_indices[i])] self.detection_index.boxes.append(box_msg) # Draw the box on top of the to_draw image class_num = int(top_label_indices[i]) if (self.class_names[class_num]=="person"): cv2.rectangle(to_draw, (xmin, ymin), (xmax, ymax), self.class_colors[class_num], 2) text = self.class_names[class_num] + " " + ('%.2f' % top_conf[i]) text_top = (xmin, ymin-10) text_bot = (xmin + 80, ymin + 5) text_pos = (xmin + 5, ymin) cv2.rectangle(to_draw, text_top, text_bot, self.class_colors[class_num], -1) cv2.putText(to_draw, text, text_pos, cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0,0,0), 1) #cv2.circle(to_draw, (xmax, ymax),1,self.class_colors[class_num],30); self.detection_index.header = std_msgs.msg.Header() self.detection_index.header.stamp=rospy.Time.now() print (self.detection_index) self.box_coordinate_pub.publish(self.detection_index) self.detection_index.boxes[:]=[] #self.detection_index.boxes.clear() cv2.imshow("SSD result", to_draw) cv2.waitKey(1) def main(self): rospy.spin()
import keras import pickle from videotest import VideoTest import sys sys.path.append("..") from ssd import SSD300 as SSD input_shape = (300,300,3) # Change this if you run with other classes than VOC class_names = ["background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]; NUM_CLASSES = len(class_names) model = SSD(input_shape, num_classes=NUM_CLASSES) # Change this path if you want to use your own trained weights model.load_weights('../weights_SSD300.hdf5') vid_test = VideoTest(class_names, model, input_shape) # To test on webcam 0, remove the parameter (or change it to another number # to test on that webcam) vid_test.run('path/to/your/video.mkv')
import keras import pickle from videotest import VideoTest import sys sys.path.append("..") from ssd import SSD300 as SSD input_shape = (300, 300, 3) # Change this if you run with other classes than VOC class_names = [ "background", "dog", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor" ] NUM_CLASSES = len(class_names) model = SSD(input_shape, num_classes=NUM_CLASSES) # Change this path if you want to use your own trained weights model.load_weights('../weights_SSD300.hdf5') vid_test = VideoTest(class_names, model, input_shape) # To test on webcam 0, remove the parameter (or change it to another number # to test on that webcam) vid_test.run('path/to/your/video.mkv')
import keras import pickle from videotest import VideoTest import sys sys.path.append("..") from ssd import SSD300 as SSD input_shape = (300, 300, 3) # Change this if you run with other classes than VOC class_names = [ "background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor" ] NUM_CLASSES = len(class_names) model = SSD(input_shape, num_classes=NUM_CLASSES) # Change this path if you want to use your own trained weights model.load_weights('./checkpoints/weights.200-3.67.hdf5') vid_test = VideoTest(class_names, model, input_shape) # To test on webcam 0, remove the parameter (or change it to another number # to test on that webcam) vid_test.run()
cv2.imshow("SSD result", orig_image) if cv2.waitKey(5) & 0xFF == ord('s'): if len(image_stack) == frame_number: if not os.path.exists(save_path + str(sample_count + 1)): os.mkdir(save_path + str(sample_count + 1)) for pic in range(frame_number): cv2.imwrite( save_path + str(sample_count + 1) + '/' + str(1000 + pic) + '.jpg', image_stack[pic]) print('saving ' + save_path + str(sample_count + 1) + '/' + str(1000 + pic) + '.jpg') image_stack = [] empty_count = 0 sample_count += 1 if __name__ == '__main__': action_class = 'stand/' root_path = 'images/' save_path = root_path + action_class if not os.path.exists(root_path): os.mkdir(root_path) if not os.path.exists(save_path): os.mkdir(save_path) save_frames = 16 input_shape = (300, 300, 3) ssd_model = SSD(input_shape, num_classes=21) ssd_model.load_weights('weights_SSD300.hdf5') run_camera(input_shape, ssd_model, save_path, save_frames)
import keras import pickle from videotest import VideoTest import pandas as pd import sys sys.path.append("..") from ssd import SSD300 as SSD input_shape = (300, 300, 3) # Change this if you run with other classes than VOC class_names = ['SS','RN','RM','SM','SY','TO','YY', 'HU'] NUM_CLASSES = len(class_names) model = SSD(input_shape, num_classes=NUM_CLASSES) # Change this path if you want to use your own trained weights # model.load_weights('../weights_SSD300.hdf5') # model.load_weights('../checkpoints/weights.04-2.58.hdf5') model.load_weights('../checkpoints//weights.10-5.21.hdf5', by_name=True) vid_test = VideoTest(class_names, model, input_shape) vid_test.run_img(img_path='../dataset/JPEGImages/', conf_thresh=0.75)
import keras import pickle from videotest import VideoTest import sys sys.path.append("..") from ssd import SSD300 as SSD input_shape = (300, 300, 3) # Change this if you run with other classes than VOC # class_names = ["background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]; class_names = ["background", "bottle", "crashedbottle"] NUM_CLASSES = len(class_names) model = SSD(input_shape, num_classes=NUM_CLASSES) # Change this path if you want to use your own trained weights # model.load_weights('../weights_SSD300.hdf5') model.load_weights('../models/1359.hdf5') vid_test = VideoTest(class_names, model, input_shape) # To test on webcam 0, remove the parameter (or change it to another number # to test on that webcam) vid_test.run(0)
from videotest import VideoTest import sys sys.path.append("..") from ssd import SSD300 as SSD input_shape = (300, 300, 3) # Change this if you run with other classes than VOC # class_names = ["background", "beansprouts", "carrot", "daikon", "enoki", "greenpepper", "shimeji"]; class_names = [ "background", "Asparagus", "Apple", "Broccoli", "Beansprouts", "Beaf", "Celery", "Carrot", "Cabbage", "Cucumber", "Chicken", "Daikon", "Egg", "Enoki", "Firefly_Squid", "Greenpepper", "Leek", "Milk", "Mushroom", "Onion", "Pumpkin", "Orange", "Pork", "Paprika", "Spinach", "Shimeji", "Squid", "Tomato", "Shimeji", "Turnip", "Ume" ] NUM_CLASSES = len(class_names) model = SSD(input_shape, num_classes=NUM_CLASSES) # Change this path if you want to use your own trained weights model.load_weights('..\checkpoints\weights.29-6.89.hdf5') vid_test = VideoTest(class_names, model, input_shape) # To test on webcam 0, remove the parameter (or change it to another number # to test on that webcam) # vid_test.run(0) vid_test.run('C:\\Users\\eisuke takahashi\\Desktop\\VID_20190310_230824.mp4')
import threading import numpy as np input_shape = (300, 300, 3) # global class_names_2send # ---------define the class to speaker # class_names_2send = '' # ---------send the class to speaker # Change this if you run with other classes than VOC class_names = ["background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]; NUM_CLASSES = len(class_names) model = SSD(input_shape, num_classes=NUM_CLASSES) # Change this path if you want to use your own trained weights model.load_weights('/home/wxf/weights_SSD300.hdf5') print 'load comlete' """ file: recv.py socket service """ vid_test = VideoTest(class_names, model, input_shape) def socket_service(): try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # AF_INET ipv4 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind(('192.168.0.105', 6666)) s.listen(10)