forked from haxko/NE3-Scope
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpcap_parser.py
More file actions
executable file
·69 lines (61 loc) · 2.22 KB
/
pcap_parser.py
File metadata and controls
executable file
·69 lines (61 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python3
import sys
import argparse
import cv2
import numpy as np
from scapy.all import *
from ne3 import Header, Frame
def rotate_image(image, angle):
image_center = tuple(np.array(image.shape[1::-1]) / 2)
rot_mat = cv2.getRotationMatrix2D(image_center, angle, 1.0)
result = cv2.warpAffine(image, rot_mat, image.shape[1::-1], flags=cv2.INTER_LINEAR)
return result
ip = "192.168.169.1"
port = 8800
parser = argparse.ArgumentParser(description="A Python based open source viewer for the NE3 Earpick wireless endoscope")
# Add command-line arguments
parser.add_argument('-v', '--verbose', action='store_true', help='Enable verbose mode')
parser.add_argument('-r', '--rotation', action='store_true', help='Rotate image')
parser.add_argument('-z', '--zoom', default=1.0, type=float, help='Zoom image')
parser.add_argument("filename", help="path to pacp")
args = parser.parse_args()
current_frame = None
current_angle = 0
for packet in rdpcap(args.filename):
if IP not in packet:
continue
if packet[IP].src != ip:
continue
if UDP not in packet:
continue
data = bytes(packet[UDP].payload)
if data[0] != 0x93:
continue
if data[1] == 0x04:
# TODO ctlmsg
continue
if data[1] != 0x01:
continue
header = Header(data)
if not current_frame:
current_frame = Frame(header)
if header.img_number > current_frame.header.img_number:
#Started receiving new image. Clear buffer.
current_frame = Frame(header)
if header.img_number < current_frame.header.img_number:
#Received older image than displayed - discard
continue
current_frame.add(data)
if current_frame.complete():
#Image RX complete
if args.verbose:
print("img_number:", current_frame.header.img_number, file=sys.stderr)
nparr = np.frombuffer(current_frame.data(), dtype=np.uint8)
img_cv = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
img_cv = cv2.resize(img_cv, None, fx=args.zoom, fy=args.zoom)
if args.rotation:
img2_cv = rotate_image(img_cv, current_frame.angle * -1 - 90)
else:
img2_cv = img_cv
cv2.imshow('image', img2_cv)
cv2.waitKey(2)