|
7 | 7 | import zipfile
|
8 | 8 | import time
|
9 | 9 | import cv2
|
| 10 | +import yaml |
10 | 11 |
|
11 | 12 | from Xlib import display, X
|
12 | 13 |
|
13 | 14 | from collections import defaultdict
|
14 | 15 | from io import StringIO
|
15 |
| -#from PIL import Image |
| 16 | +from PIL import Image |
16 | 17 |
|
17 |
| -cap = cv2.VideoCapture(0) |
| 18 | +#cap = cv2.VideoCapture(0) |
18 | 19 | #cap = cv2.VideoCapture('../opencv_extra/testdata/highgui/video/big_buck_bunny.mp4')
|
19 | 20 |
|
20 | 21 | sys.path.append('../tensorflow_models/research')
|
|
24 | 25 | from utils import label_map_util
|
25 | 26 | from utils import visualization_utils as vis_util
|
26 | 27 |
|
| 28 | + |
| 29 | +# Load config values from config.obj_detect.sample.yml (as default values) updated by optional user-specific config.obj_detect.yml |
| 30 | +## see also http://treyhunner.com/2016/02/how-to-merge-dictionaries-in-python/ |
| 31 | +cfg = yaml.load(open("config/config.obj_detect.sample.yml", 'r')) |
| 32 | +if os.path.isfile("config/config.obj_detect.yml"): |
| 33 | + cfg_user = yaml.load(open("config/config.obj_detect.yml", 'r')) |
| 34 | + cfg.update(cfg_user) |
| 35 | +#for section in cfg: |
| 36 | +# print(section, ":", cfg[section]) |
| 37 | + |
| 38 | + |
| 39 | + |
27 | 40 | # Any model exported using the `export_inference_graph.py` tool can be loaded here simply by changing `PATH_TO_CKPT` to point to a new .pb file.
|
28 | 41 | # See the [detection model zoo](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md) for a list of other models that can be run out-of-the-box with varying speeds and accuracies.
|
29 | 42 |
|
30 |
| -# What model to download. |
31 |
| -MODEL_NAME = 'ssd_mobilenet_v1_coco_11_06_2017' |
32 |
| -MODEL_FILE = MODEL_NAME + '.tar.gz' |
33 |
| -DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/' |
34 |
| - |
35 | 43 | # Path to frozen detection graph. This is the actual model that is used for the object detection.
|
36 |
| -PATH_TO_CKPT = '../' + MODEL_NAME + '/frozen_inference_graph.pb' |
| 44 | +PATH_TO_CKPT = '../' + cfg['model_name'] + '/frozen_inference_graph.pb' |
37 | 45 |
|
38 | 46 | # List of the strings that is used to add correct label for each box.
|
39 | 47 | PATH_TO_LABELS = os.path.join('../tensorflow_models/research/object_detection/data', 'mscoco_label_map.pbtxt')
|
40 | 48 |
|
41 | 49 | NUM_CLASSES = 90
|
42 | 50 |
|
43 | 51 | # ## Download Model
|
| 52 | +MODEL_FILE = cfg['model_name'] + cfg['model_dl_file_format'] |
44 | 53 | if not os.path.isfile(PATH_TO_CKPT):
|
45 | 54 | print('Model not found. We will download it now.')
|
46 | 55 | opener = urllib.request.URLopener()
|
47 |
| - opener.retrieve(DOWNLOAD_BASE + MODEL_FILE, '../' + MODEL_FILE) |
| 56 | + opener.retrieve(cfg['model_dl_base_path'] + MODEL_FILE, '../' + MODEL_FILE) |
48 | 57 | tar_file = tarfile.open('../' + MODEL_FILE)
|
49 | 58 | for file in tar_file.getmembers():
|
50 | 59 | file_name = os.path.basename(file.name)
|
|
95 | 104 |
|
96 | 105 | windowPlacedYet = False
|
97 | 106 |
|
98 |
| - while(cap.isOpened()): |
99 |
| -# while(True): |
| 107 | +# while(cap.isOpened()): |
| 108 | + while(True): |
100 | 109 |
|
101 | 110 | dsp = display.Display()
|
102 | 111 | root = dsp.screen().root
|
103 | 112 | reso = root.get_geometry()
|
104 |
| -# W,H = int(reso.width/2),int(reso.height/2) |
| 113 | + W,H = int(reso.width/2),int(reso.height/2) |
105 | 114 | #W,H = 600,600
|
106 |
| -# raw = root.get_image(0, 0, W, H, X.ZPixmap, 0xffffffff) |
107 |
| -# image = Image.frombytes("RGB", (W, H), raw.data, "raw", "RGBX") |
108 |
| -# image_np = np.array(image); |
| 115 | + raw = root.get_image(0, 0, W, H, X.ZPixmap, 0xffffffff) |
| 116 | + image = Image.frombytes("RGB", (W, H), raw.data, "raw", "RGBX") |
| 117 | + image_np = np.array(image); |
109 | 118 |
|
110 | 119 | # image_np_bgr = np.array(ImageGrab.grab(bbox=(0,0,600,600))) # grab(bbox=(10,10,500,500)) or just grab()
|
111 | 120 | # image_np = cv2.cvtColor(image_np_bgr, cv2.COLOR_BGR2RGB)
|
112 | 121 |
|
113 |
| - ret, image_np = cap.read() |
114 |
| - if not ret: |
115 |
| - print("Video finished!") |
116 |
| - break |
| 122 | +# ret, image_np = cap.read() |
| 123 | +# if not ret: |
| 124 | +# print("Video finished!") |
| 125 | +# break |
117 | 126 |
|
118 | 127 | # for image_path in TEST_IMAGE_PATHS:
|
119 | 128 | # image = Image.open(image_path)
|
|
150 | 159 | counter = 0
|
151 | 160 | start_time = time.time()
|
152 | 161 |
|
153 |
| -cap.release() |
| 162 | +#cap.release() |
154 | 163 | cv2.destroyAllWindows()
|
0 commit comments