File size: 2,771 Bytes
07f5340
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
70
71
72
73
74
75
"""Per-frame quality metrics for the whole video + review contact sheets.

Usage: python 01_frame_metrics.py <video> <out_csv> <sheet_dir>

Metrics (every source frame):
  sharpness    variance of Laplacian on the grey image (higher = sharper)
  brightness   mean grey level 0-255
  red_ratio    mean R / (R+G+B); intracorporeal tissue is strongly red
  green_ratio  mean G / (R+G+B)
  saturated    fraction of pixels with grey >= 250 (glare / light flooding)
  dark         fraction of pixels with grey <= 15
  motion       mean absolute grey difference to the previous frame
"""
import csv
import sys
from pathlib import Path

import cv2
import numpy as np

video, out_csv, sheet_dir = sys.argv[1], sys.argv[2], Path(sys.argv[3])
sheet_dir.mkdir(parents=True, exist_ok=True)

cap = cv2.VideoCapture(video)
fps = cap.get(cv2.CAP_PROP_FPS)
SHEET_STEP_S = 4          # one thumbnail every 4 s for the overview sheets
COLS, ROWS, TW, TH = 5, 6, 288, 230
thumbs = []
prev = None
rows = []
idx = 0
while True:
    ok, frame = cap.read()
    if not ok:
        break
    grey = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    small = cv2.resize(grey, (180, 144), interpolation=cv2.INTER_AREA)
    b, g, r = [c.astype(np.float64).mean() for c in cv2.split(frame)]
    total = r + g + b + 1e-9
    motion = 0.0 if prev is None else float(np.abs(small.astype(np.int16) - prev).mean())
    prev = small.astype(np.int16)
    rows.append([
        idx, round(idx / fps, 3),
        round(float(cv2.Laplacian(grey, cv2.CV_64F).var()), 2),
        round(float(grey.mean()), 2),
        round(r / total, 4), round(g / total, 4),
        round(float((grey >= 250).mean()), 4),
        round(float((grey <= 15).mean()), 4),
        round(motion, 3),
    ])
    if idx % int(round(fps * SHEET_STEP_S)) == 0:
        t = cv2.resize(frame, (TW, TH), interpolation=cv2.INTER_AREA)
        cv2.rectangle(t, (0, 0), (TW, 16), (0, 0, 0), -1)
        cv2.putText(t, f"{idx / fps:6.1f}s  f{idx}", (4, 12), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1)
        thumbs.append(t)
    idx += 1
cap.release()

with open(out_csv, "w", newline="") as f:
    w = csv.writer(f)
    w.writerow(["frame_index", "timestamp_seconds", "sharpness", "brightness", "red_ratio",
                "green_ratio", "saturated", "dark", "motion"])
    w.writerows(rows)

per = COLS * ROWS
for s in range(0, len(thumbs), per):
    chunk = thumbs[s:s + per]
    sheet = np.zeros((ROWS * TH, COLS * TW, 3), np.uint8)
    for i, t in enumerate(chunk):
        y, x = divmod(i, COLS)
        sheet[y * TH:(y + 1) * TH, x * TW:(x + 1) * TW] = t
    cv2.imwrite(str(sheet_dir / f"overview_{s // per:02d}.jpg"), sheet, [cv2.IMWRITE_JPEG_QUALITY, 85])

print("frames", idx, "fps", fps, "sheets", (len(thumbs) + per - 1) // per)