Skip to content

Commit 5760ee8

Browse files
author
dbickson
committed
adding python sources
1 parent f61ce86 commit 5760ee8

File tree

7 files changed

+1446
-0
lines changed

7 files changed

+1446
-0
lines changed

python/__init__.py

Lines changed: 523 additions & 0 deletions
Large diffs are not rendered by default.

python/capture_io.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
2+
# FastDup Software, (C) copyright 2022 Dr. Amir Alush and Dr. Danny Bickson.
3+
# This software is free for non-commercial and academic usage under the Creative Common Attribution-NonCommercial-NoDerivatives
4+
# 4.0 International license. Please reach out to [email protected] for licensing options.
5+
6+
# workaround taken from https://stackoverflow.com/questions/35745541/how-to-get-printed-output-from-ctypes-c-functions-into-jupyter-ipython-notebook
7+
# to view C++ output in jupyter notebook
8+
9+
import io
10+
import os
11+
import sys
12+
import tempfile
13+
from contextlib import contextmanager
14+
import ctypes
15+
from ctypes import util
16+
import platform
17+
# use_errno parameter is optional, because I'm not checking errno anyway.
18+
libc = ctypes.CDLL(util.find_library('c'), use_errno=True)
19+
20+
class FILE(ctypes.Structure):
21+
pass
22+
23+
FILE_p = ctypes.POINTER(FILE)
24+
25+
# Alternatively, we can just use:
26+
# FILE_p = ctypes.c_void_p
27+
28+
# These variables, defined inside the C library, are readonly.
29+
if platform.system() == "Darwin":
30+
cstdin = FILE_p.in_dll(libc, '__stdinp')
31+
cstdout = FILE_p.in_dll(libc, '__stdoutp')
32+
cstderr = FILE_p.in_dll(libc, '__stderrp')
33+
else:
34+
cstdin = FILE_p.in_dll(libc, 'stdin')
35+
cstdout = FILE_p.in_dll(libc, 'stdout')
36+
cstderr = FILE_p.in_dll(libc, 'stderr')
37+
38+
# C function to disable buffering.
39+
csetbuf = libc.setbuf
40+
csetbuf.argtypes = (FILE_p, ctypes.c_char_p)
41+
csetbuf.restype = None
42+
43+
# C function to flush the C library buffer.
44+
cfflush = libc.fflush
45+
cfflush.argtypes = (FILE_p,)
46+
cfflush.restype = ctypes.c_int
47+
48+
@contextmanager
49+
def capture_c_stdout(encoding='utf8'):
50+
# Flushing, it's a good practice.
51+
sys.stdout.flush()
52+
sys.stderr.flush()
53+
cfflush(cstdout)
54+
cfflush(cstderr)
55+
56+
# We need to use a actual file because we need the file descriptor number.
57+
with tempfile.TemporaryFile(buffering=0) as temp:
58+
# Saving a copy of the original stdout.
59+
with tempfile.TemporaryFile(buffering=0) as temp2:
60+
prev_sys_stdout = sys.stdout
61+
prev_sys_stderr = sys.stderr
62+
prev_stdout_fd = os.dup(1)
63+
prev_stderr_fd = os.dup(2)
64+
os.close(1)
65+
os.close(2)
66+
67+
# Duplicating the temporary file fd into the stdout fd.
68+
# In other words, replacing the stdout.
69+
os.dup2(temp.fileno(), 1)
70+
os.dup2(temp2.fileno(), 2)
71+
72+
# Replacing sys.stdout for Python code.
73+
#
74+
# IPython Notebook version of sys.stdout is actually an
75+
# in-memory OutStream, so it does not have a file descriptor.
76+
# We need to replace sys.stdout so that interleaved Python
77+
# and C output gets captured in the correct order.
78+
#
79+
# We enable line_buffering to force a flush after each line.
80+
# And write_through to force all data to be passed through the
81+
# wrapper directly into the binary temporary file.
82+
temp_wrapper = io.TextIOWrapper(
83+
temp, encoding=encoding, line_buffering=True, write_through=True)
84+
sys.stdout = temp_wrapper
85+
temp_wrapper2 = io.TextIOWrapper(temp2, encoding=encoding, line_buffering=True, write_through=True)
86+
sys.stderr = temp_wrapper2
87+
88+
# Disabling buffering of C stdout.
89+
csetbuf(cstdout, None)
90+
csetbuf(cstderr, None)
91+
92+
yield
93+
94+
# Must flush to clear the C library buffer.
95+
cfflush(cstdout)
96+
cfflush(cstderr)
97+
98+
# Restoring stdout.
99+
os.dup2(prev_stdout_fd, 1)
100+
os.dup2(prev_stderr_fd, 2)
101+
os.close(prev_stdout_fd)
102+
os.close(prev_stderr_fd)
103+
sys.stdout = prev_sys_stdout
104+
sys.stderr = prev_sys_stderr
105+
106+
# Printing the captured output.
107+
temp_wrapper.seek(0)
108+
print(temp_wrapper.read(), end='')
109+
temp_wrapper2.seek(0)
110+
print(temp_wrapper2.read(), end='')

python/coco.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
2+
# FastDup Software, (C) copyright 2022 Dr. Amir Alush and Dr. Danny Bickson.
3+
# This software is free for non-commercial and academic usage under the Creative Common Attribution-NonCommercial-NoDerivatives
4+
# 4.0 International license. Please reach out to [email protected] for licensing options.
5+
6+
7+
import os
8+
cat = {0: u'__background__',
9+
1: u'person',
10+
2: u'bicycle',
11+
3: u'car',
12+
4: u'motorcycle',
13+
5: u'airplane',
14+
6: u'bus',
15+
7: u'train',
16+
8: u'truck',
17+
9: u'boat',
18+
10: u'traffic light',
19+
11: u'fire hydrant',
20+
12: u'stop sign',
21+
13: u'parking meter',
22+
14: u'bench',
23+
15: u'bird',
24+
16: u'cat',
25+
17: u'dog',
26+
18: u'horse',
27+
19: u'sheep',
28+
20: u'cow',
29+
21: u'elephant',
30+
22: u'bear',
31+
23: u'zebra',
32+
24: u'giraffe',
33+
25: u'backpack',
34+
26: u'umbrella',
35+
27: u'handbag',
36+
28: u'tie',
37+
29: u'suitcase',
38+
30: u'frisbee',
39+
31: u'skis',
40+
32: u'snowboard',
41+
33: u'sports ball',
42+
34: u'kite',
43+
35: u'baseball bat',
44+
36: u'baseball glove',
45+
37: u'skateboard',
46+
38: u'surfboard',
47+
39: u'tennis racket',
48+
40: u'bottle',
49+
41: u'wine glass',
50+
42: u'cup',
51+
43: u'fork',
52+
44: u'knife',
53+
45: u'spoon',
54+
46: u'bowl',
55+
47: u'banana',
56+
48: u'apple',
57+
49: u'sandwich',
58+
50: u'orange',
59+
51: u'broccoli',
60+
52: u'carrot',
61+
53: u'hot dog',
62+
54: u'pizza',
63+
55: u'donut',
64+
56: u'cake',
65+
57: u'chair',
66+
58: u'couch',
67+
59: u'potted plant',
68+
60: u'bed',
69+
61: u'dining table',
70+
62: u'toilet',
71+
63: u'tv',
72+
64: u'laptop',
73+
65: u'mouse',
74+
66: u'remote',
75+
67: u'keyboard',
76+
68: u'cell phone',
77+
69: u'microwave',
78+
70: u'oven',
79+
71: u'toaster',
80+
72: u'sink',
81+
73: u'refrigerator',
82+
74: u'book',
83+
75: u'clock',
84+
76: u'vase',
85+
77: u'scissors',
86+
78: u'teddy bear',
87+
79: u'hair drier',
88+
80: u'toothbrush'}
89+
90+
91+
92+
def read_coco_labels(path):
93+
label_dict = {}
94+
files = os.listdir(path)
95+
for f in files:
96+
if f.endswith('.txt'):
97+
try:
98+
with open(os.path.join(path, f)) as w:
99+
line = w.read()
100+
if (line.strip() == ''):
101+
continue
102+
int_cat = int(line.split(' ')[0])
103+
#print('cat is', cat[int_cat])
104+
label_dict[os.path.join(path.replace('labels','images'),f).replace('.txt','.jpg')] = cat[int_cat]
105+
except Exception as ex:
106+
print('Failed to read file ', os.path.join(path, f), ' with error: ', ex)
107+
108+
return label_dict
109+
110+

0 commit comments

Comments
 (0)