|
| 1 | +import { useCallback, useMemo, useState } from "react"; |
| 2 | +import { LuCornerDownLeft } from "react-icons/lu"; |
| 3 | +import { ExclamationCircleIcon } from "@heroicons/react/16/solid"; |
| 4 | +import { type LoggerMessage } from "tesseract.js"; |
| 5 | + |
| 6 | +import { Button } from "@components/Button"; |
| 7 | +import { GridCard } from "@components/Card"; |
| 8 | +import { TextAreaWithLabel } from "@components/TextArea"; |
| 9 | +import { SettingsPageHeader } from "@components/SettingsPageheader"; |
| 10 | +import { useUiStore } from "@/hooks/stores"; |
| 11 | +import useOCR from "@/hooks/useOCR"; |
| 12 | +import { cx } from "@/cva.config"; |
| 13 | + |
| 14 | +export default function OCRModal({ videoElmRef }: { videoElmRef: React.RefObject<HTMLVideoElement> }) { |
| 15 | + const { setDisableVideoFocusTrap } = useUiStore(); |
| 16 | + const [ocrStatus, setOcrStatus] = useState<LoggerMessage>(); |
| 17 | + const [ocrError, setOcrError] = useState<string | null>(null); |
| 18 | + const handleOcrError = useCallback((error: any) => { // eslint-disable-line @typescript-eslint/no-explicit-any |
| 19 | + if (typeof error === "string") { |
| 20 | + setOcrError(error); |
| 21 | + } else { |
| 22 | + setOcrError(error.message); |
| 23 | + } |
| 24 | + }, [setOcrError]); |
| 25 | + const [ocrText, setOcrText] = useState<string | null>(null); |
| 26 | + |
| 27 | + const { ocrImage } = useOCR(); |
| 28 | + |
| 29 | + const onConfirmOCR = useCallback(async () => { |
| 30 | + setDisableVideoFocusTrap(true); |
| 31 | + |
| 32 | + setOcrText(null); |
| 33 | + setOcrError(null); |
| 34 | + setOcrStatus(undefined); |
| 35 | + |
| 36 | + if (!videoElmRef.current) { |
| 37 | + setOcrError("Video element not found"); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + setOcrStatus({ |
| 42 | + status: "Capturing image", |
| 43 | + progress: 0, |
| 44 | + jobId: "", |
| 45 | + userJobId: "", |
| 46 | + workerId: "", |
| 47 | + }); |
| 48 | + |
| 49 | + // create a canvas from the video element then capture the image from the canvas |
| 50 | + const video = videoElmRef.current; |
| 51 | + const canvas = document.createElement("canvas"); |
| 52 | + canvas.width = video.videoWidth; |
| 53 | + canvas.height = video.videoHeight; |
| 54 | + |
| 55 | + console.log(video.width, video.height, video) |
| 56 | + |
| 57 | + const ctx = canvas.getContext("2d"); |
| 58 | + ctx?.drawImage(video, 0, 0, canvas.width, canvas.height); |
| 59 | + |
| 60 | + const text = await ocrImage(["eng"], canvas, { logger: setOcrStatus, errorHandler: handleOcrError }); |
| 61 | + setOcrText(text); |
| 62 | + |
| 63 | + setOcrStatus(undefined); |
| 64 | + setOcrError(null); |
| 65 | + }, [ |
| 66 | + videoElmRef, |
| 67 | + setDisableVideoFocusTrap, |
| 68 | + ocrImage, |
| 69 | + setOcrStatus, |
| 70 | + setOcrError, |
| 71 | + handleOcrError, |
| 72 | + ]); |
| 73 | + |
| 74 | + const ocrProgress = useMemo(() => { |
| 75 | + if (!ocrStatus?.progress) return 0; |
| 76 | + return Math.round(ocrStatus?.progress * 100); |
| 77 | + }, [ocrStatus]); |
| 78 | + |
| 79 | + return ( |
| 80 | + <GridCard> |
| 81 | + <div className="space-y-4 p-4 py-3"> |
| 82 | + <div className="grid h-full grid-rows-(--grid-headerBody)"> |
| 83 | + <div className="h-full space-y-4"> |
| 84 | + <div className="space-y-4"> |
| 85 | + <SettingsPageHeader |
| 86 | + title="OCR" |
| 87 | + description="OCR text from the video" |
| 88 | + /> |
| 89 | + |
| 90 | + <div |
| 91 | + className={cx("animate-fadeIn space-y-2 opacity-0", ocrText === null ? "hidden" : "")} |
| 92 | + style={{ |
| 93 | + animationDuration: "0.7s", |
| 94 | + animationDelay: "0.1s", |
| 95 | + }} |
| 96 | + > |
| 97 | + <div> |
| 98 | + <div |
| 99 | + className="w-full" |
| 100 | + onKeyUp={e => e.stopPropagation()} |
| 101 | + onKeyDown={e => e.stopPropagation()} |
| 102 | + > |
| 103 | + <TextAreaWithLabel |
| 104 | + value={ocrText || ""} |
| 105 | + label="Text" |
| 106 | + rows={4} |
| 107 | + readOnly |
| 108 | + spellCheck={false} |
| 109 | + data-lt="false" |
| 110 | + data-gram="false" |
| 111 | + /> |
| 112 | + </div> |
| 113 | + </div> |
| 114 | + </div> |
| 115 | + |
| 116 | + {ocrStatus && <div className={cx("animate-fadeIn space-y-2 opacity-0")} |
| 117 | + style={{ |
| 118 | + animationDuration: "0.7s", |
| 119 | + animationDelay: "0.1s", |
| 120 | + }} |
| 121 | + > |
| 122 | + <div className="space-y-1 flex justify-between"> |
| 123 | + <p className="text-xs text-slate-600 dark:text-slate-400 capitalize"> |
| 124 | + {ocrStatus?.status} |
| 125 | + </p> |
| 126 | + <span className="text-xs text-slate-600 dark:text-slate-400">{ocrProgress}%</span> |
| 127 | + </div> |
| 128 | + <div className="h-2.5 w-full overflow-hidden rounded-full bg-slate-300"> |
| 129 | + <div |
| 130 | + style={{ width: ocrProgress + "%" }} |
| 131 | + className="h-2.5 bg-blue-700 transition-all duration-1000 ease-in-out" |
| 132 | + ></div> |
| 133 | + </div> |
| 134 | + </div>} |
| 135 | + |
| 136 | + {ocrError && ( |
| 137 | + <div className="flex items-center gap-x-2"> |
| 138 | + <ExclamationCircleIcon className="h-4 w-4 text-red-500 dark:text-red-400" /> |
| 139 | + <span className="text-xs text-red-500 dark:text-red-400"> |
| 140 | + {ocrError} |
| 141 | + </span> |
| 142 | + </div> |
| 143 | + )} |
| 144 | + </div> |
| 145 | + </div> |
| 146 | + </div> |
| 147 | + <div className="gap-y-4"> |
| 148 | + <p className="text-xs text-slate-600 dark:text-slate-400"> |
| 149 | + Internet connectivity might be required to download Tesseract OCR trained data. |
| 150 | + </p> |
| 151 | + </div> |
| 152 | + <div className="flex animate-fadeIn items-center justify-end gap-x-2 opacity-0" |
| 153 | + style={{ |
| 154 | + animationDuration: "0.7s", |
| 155 | + animationDelay: "0.2s", |
| 156 | + }} |
| 157 | + > |
| 158 | + <Button |
| 159 | + size="SM" |
| 160 | + theme="primary" |
| 161 | + text="Start OCR" |
| 162 | + onClick={onConfirmOCR} |
| 163 | + LeadingIcon={LuCornerDownLeft} |
| 164 | + /> |
| 165 | + </div> |
| 166 | + </div> |
| 167 | + </GridCard> |
| 168 | + ); |
| 169 | +} |
0 commit comments