()
| 13 | // `; |
| 14 | |
| 15 | export default function Viewer() { |
| 16 | const webcamRef = useRef(null); |
| 17 | const [capturedImg, setCapturedImg] = useState(null); |
| 18 | const [extracted, setExtracted] = useState(""); |
| 19 | |
| 20 | const [isPaused, setPause] = useState(false); |
| 21 | const ws = useRef(null); |
| 22 | |
| 23 | useEffect(() => { |
| 24 | setPause(false); |
| 25 | const client_id = Date.now(); |
| 26 | const url = `${config.WS_SERVER}/${client_id}`; |
| 27 | ws.current = new WebSocket(url); |
| 28 | |
| 29 | // console.log(url); |
| 30 | // ws.current.onopen = () => console.log("ws opened"); |
| 31 | // ws.current.onclose = () => console.log("ws closed"); |
| 32 | |
| 33 | return () => { |
| 34 | ws.current.close(); |
| 35 | }; |
| 36 | }, []); |
| 37 | |
| 38 | useEffect(() => { |
| 39 | if (!ws.current) return; |
| 40 | |
| 41 | ws.current.onmessage = (event) => { |
| 42 | if (isPaused) return; |
| 43 | const message = JSON.parse(event.data); |
| 44 | // console.log(message); |
| 45 | var text = message["extracted"]; |
| 46 | var img = message["processed_img"]; |
| 47 | setExtracted(text); |
| 48 | setCapturedImg(img); |
| 49 | }; |
| 50 | }, [isPaused]); |
| 51 | |
| 52 | function sendMessage(msg) { |
| 53 | if (!ws.current) return; |
| 54 | |
| 55 | ws.current.send(msg); |
| 56 | } |
| 57 | |
| 58 | const videoConstraints = { |
| 59 | width: 1280, |
| 60 | height: 720, |
| 61 | facingMode: "environment", // Can be "environment" or "user" |
| 62 | screenshotQuality: 1, |
| 63 | }; |
| 64 | |
| 65 | const capture = useCallback(() => { |
| 66 | const capturedImg = webcamRef.current.getScreenshot(); |
| 67 | setCapturedImg(capturedImg); |
| 68 | setExtracted(""); |
| 69 | const data = JSON.stringify({ data: capturedImg }); |
| 70 | sendMessage(data); |
| 71 | }, [webcamRef]); |
| 72 |
nothing calls this directly
no test coverage detected