Future-Tense commited on
Commit
32d34f8
1 Parent(s): 7e3ea67

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2 # opencv2 package for python.
2
+ import pafy # pafy allows us to read videos from youtube.
3
+ from torch import hub # Hub contains other models like FasterRCNN
4
+
5
+ URL = "https://www.youtube.com/watch?v=dQw4w9WgXcQ" #URL to parse
6
+ play = pafy.new(self._URL).streams[-1] #'-1' means read the lowest quality of video.
7
+
8
+ assert play is not None # we want to make sure their is a input to read.
9
+ stream = cv2.VideoCapture(play.url) #create a opencv video stream.
10
+ #stream = cv2.VideoCapture(0) # 0 means read from local camera.
11
+ #camera_ip = "rtsp://username:password@IP/port"
12
+ #stream = cv2.VideoCapture(camera_ip)
13
+
14
+ model = torch.hub.load('ultralytics/yolov5','yolov5s',pretrained=True)
15
+
16
+ """
17
+ The function below identifies the device which is availabe to make the prediction and uses it to load and infer the frame. Once it has results it will extract the labels and cordinates(Along with scores) for each object detected in the frame.
18
+ """
19
+ def score_frame(frame, model):
20
+ device = 'cuda' if torch.cuda.is_available() else 'cpu'
21
+ model.to(device)
22
+ frame = [torch.tensor(frame)]
23
+ results = self.model(frame)
24
+ labels = results.xyxyn[0][:, -1].numpy()
25
+ cord = results.xyxyn[0][:, :-1].numpy()
26
+ return labels, cord
27
+
28
+ """
29
+ The function below takes the results and the frame as input and plots boxes over all the objects which have a score higer than our threshold.
30
+ """
31
+ def plot_boxes(self, results, frame):
32
+ labels, cord = results
33
+ n = len(labels)
34
+ x_shape, y_shape = frame.shape[1], frame.shape[0]
35
+ for i in range(n):
36
+ row = cord[i]
37
+ # If score is less than 0.2 we avoid making a prediction.
38
+ if row[4] < 0.2:
39
+ continue
40
+ x1 = int(row[0]*x_shape)
41
+ y1 = int(row[1]*y_shape)
42
+ x2 = int(row[2]*x_shape)
43
+ y2 = int(row[3]*y_shape)
44
+ bgr = (0, 255, 0) # color of the box
45
+ classes = self.model.names # Get the name of label index
46
+ label_font = cv2.FONT_HERSHEY_SIMPLEX #Font for the label.
47
+ cv2.rectangle(frame, \
48
+ (x1, y1), (x2, y2), \
49
+ bgr, 2) #Plot the boxes
50
+ cv2.putText(frame,\
51
+ classes[labels[i]], \
52
+ (x1, y1), \
53
+ label_font, 0.9, bgr, 2) #Put a label over box.
54
+ return frame
55
+
56
+ """
57
+ The Function below oracestrates the entire operation and performs the real-time parsing for video stream.
58
+ """
59
+ def __call__(self):
60
+ player = self.get_video_stream() #Get your video stream.
61
+ assert player.isOpened() # Make sure that their is a stream.
62
+ #Below code creates a new video writer object to write our
63
+ #output stream.
64
+ x_shape = int(player.get(cv2.CAP_PROP_FRAME_WIDTH))
65
+ y_shape = int(player.get(cv2.CAP_PROP_FRAME_HEIGHT))
66
+ four_cc = cv2.VideoWriter_fourcc(*"MJPG") #Using MJPEG codex
67
+ out = cv2.VideoWriter(out_file, four_cc, 20, \
68
+ (x_shape, y_shape))
69
+ ret, frame = player.read() # Read the first frame.
70
+ while rect: # Run until stream is out of frames
71
+ start_time = time() # We would like to measure the FPS.
72
+ results = self.score_frame(frame) # Score the Frame
73
+ frame = self.plot_boxes(results, frame) # Plot the boxes.
74
+ end_time = time()
75
+ fps = 1/np.round(end_time - start_time, 3) #Measure the FPS.
76
+ print(f"Frames Per Second : {fps}")
77
+ out.write(frame) # Write the frame onto the output.
78
+ ret, frame = player.read() # Read next frame.
79
+
80
+