Py.Cafe

alexlib/

streamlit-on-pycafe

Streamlit on Py.cafe

DocsPricing
  • app.py
  • requirements.txt
app.py
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
76
77
78
import streamlit as st
import cv2
import numpy as np
import matplotlib.pyplot as plt
from skimage import exposure, img_as_ubyte
from openpiv import pyprocess, piv, validation, tools, filters, scaling
from skimage.registration import optical_flow_ilk

st.title("PIV Video Processing App")

def as_grey(frame):
    red = frame[:, :, 0]
    green = frame[:, :, 1]
    blue = frame[:, :, 2]
    im = np.ceil(0.2125 * red + 0.7154 * green + 0.0721 * blue).astype(np.uint8)
    im = exposure.equalize_adapthist(im, clip_limit=1.2)
    return img_as_ubyte(im)

uploaded_file = st.file_uploader("Upload a video file", type=["mp4", "avi", "mov"])
if uploaded_file is not None:
    vidcap = cv2.VideoCapture(uploaded_file.name)
    success, image1 = vidcap.read()
    skip = 3
    for i in range(skip):
        success, image2 = vidcap.read()
    frame_a = as_grey(image1).astype(np.int32)
    frame_b = as_grey(image2).astype(np.int32)

    winsize = 64
    searchsize = 128
    overlap = 32
    dt = 1

    u0, v0, sig2noise = pyprocess.extended_search_area_piv(frame_a, frame_b, window_size=winsize, overlap=overlap, dt=dt, search_area_size=searchsize, sig2noise_method='peak2peak')
    x, y = pyprocess.get_coordinates(image_size=frame_a.shape, window_size=winsize, overlap=overlap)
    mask = validation.sig2noise_val(sig2noise, threshold=1.3)
    u1, v1 = filters.replace_outliers(u0, v0, method='localmean', max_iter=3, kernel_size=2)
    u2, v2 = scaling.uniform(x, y, u1, v1, scaling_factor=1)
    x, y, u3, v3 = tools.transform_coordinates(x, y, u2, v2)

    st.write("PIV Results")
    fig, ax = plt.subplots()
    tools.display_vector_field((x, y, u3, v3), ax=ax, scaling_factor=1, scale=1000, width=0.0035, on_img=True)
    st.pyplot(fig)

    U = np.stack([u3])
    Umean = np.mean(U, axis=0)
    V = np.stack([v3])
    Vmean = np.mean(V, axis=0)

    fig, ax = plt.subplots(figsize=(8, 8))
    ax.imshow(image1, alpha=0.7)
    ax.quiver(x, y, Umean, Vmean, scale=200, color='r', width=.008)
    plt.plot(np.mean(Umean, axis=1) * 20, y[:, 0], color='y', lw=3)
    st.pyplot(fig)

    v, u = optical_flow_ilk(frame_a, frame_b, radius=15)
    norm = np.sqrt(u ** 2 + v ** 2)

    fig, (ax0, ax1) = plt.subplots(1, 2, figsize=(8, 4))
    ax0.imshow(frame_a, cmap='gray')
    ax0.set_title("Sequence image sample")
    ax0.set_axis_off()

    nvec = 20
    nl, nc = frame_a.shape
    step = max(nl // nvec, nc // nvec)
    y, x = np.mgrid[:nl:step, :nc:step]
    u_ = u[::step, ::step]
    v_ = v[::step, ::step]

    ax1.imshow(norm)
    ax1.quiver(x, y, u_, v_, color='r', units='dots', angles='xy', scale_units='xy', lw=3)
    ax1.set_title("Optical flow magnitude and vector field")
    ax1.set_axis_off()
    fig.tight_layout()
    st.pyplot(fig)