Skip to content

Commit

Permalink
Merge pull request #82 from coronasafe/ocr
Browse files Browse the repository at this point in the history
Get OCR working
  • Loading branch information
mathew-alex authored Aug 25, 2023
2 parents 1c53f50 + ddbfd79 commit 5c013d4
Show file tree
Hide file tree
Showing 16 changed files with 659 additions and 176 deletions.
21 changes: 0 additions & 21 deletions prisma/migrations/20230619073305_ocr_fields_added/migration.sql

This file was deleted.

44 changes: 44 additions & 0 deletions prisma/migrations/20230818140017_add_bed/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
-- CreateEnum
CREATE TYPE "AssetType" AS ENUM ('CAMERA', 'MONITOR');

-- AlterTable
ALTER TABLE "Asset" ADD COLUMN "bedId" INTEGER,
ADD COLUMN "password" TEXT,
ADD COLUMN "port" INTEGER DEFAULT 80,
ADD COLUMN "type" "AssetType" NOT NULL DEFAULT 'MONITOR',
ADD COLUMN "username" TEXT;

-- CreateTable
CREATE TABLE "Bed" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,
"externalId" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"deleted" BOOLEAN NOT NULL DEFAULT false,

CONSTRAINT "Bed_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Preset" (
"id" SERIAL NOT NULL,
"x" INTEGER NOT NULL,
"y" INTEGER NOT NULL,
"zoom" INTEGER NOT NULL DEFAULT 0,
"bedId" INTEGER NOT NULL,

CONSTRAINT "Preset_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "Bed_externalId_key" ON "Bed"("externalId");

-- CreateIndex
CREATE UNIQUE INDEX "Preset_bedId_key" ON "Preset"("bedId");

-- AddForeignKey
ALTER TABLE "Asset" ADD CONSTRAINT "Asset_bedId_fkey" FOREIGN KEY ("bedId") REFERENCES "Bed"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Preset" ADD CONSTRAINT "Preset_bedId_fkey" FOREIGN KEY ("bedId") REFERENCES "Bed"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
35 changes: 26 additions & 9 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ datasource db {
url = env("DATABASE_URL")
}

enum AssetType {
CAMERA
MONITOR
}

model Asset {
id Int @id @default(autoincrement())
name String
type AssetType @default(MONITOR)
description String
externalId String @unique
ipAddress String @unique
Expand All @@ -21,21 +27,32 @@ model Asset {
deleted Boolean @default(false)
username String?
password String?
port Int?
port Int? @default(80)
DailyRound DailyRound[]
Bed Bed? @relation(fields: [bedId], references: [id])
bedId Int?
@@index([externalId, ipAddress])
}

model Bed {
id Int @id @default(autoincrement())
patientExternalId String @unique
x Int
y Int
zoom Int @default(0)
createdAt DateTime @default(now())
updatedAt DateTime @default(now())
deleted Boolean @default(false)
id Int @id @default(autoincrement())
name String
externalId String @unique
monitorPreset Preset?
assets Asset[]
createdAt DateTime @default(now())
updatedAt DateTime @default(now())
deleted Boolean @default(false)
}

model Preset {
id Int @id @default(autoincrement())
x Int
y Int
zoom Int @default(0)
bed Bed @relation(fields: [bedId], references: [id])
bedId Int @unique
}

model DailyRound {
Expand Down
100 changes: 50 additions & 50 deletions src/automation/autoDataExtractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,39 +60,42 @@ const getSanitizedData = (data)=>{

}

const extractData = async (camParams, patientId)=>{

const coordinates = await getMonitorCoordinates(patientId)
await CameraUtils.absoluteMove({ camParams, ...coordinates })

const snapshotUrl = await CameraUtils.getSnapshotUri({ camParams });

const fileName = "image-" + new Date().getTime() + ".jpeg"
const imagePath = path.resolve("images", fileName)
await downloadImage(snapshotUrl.uri, imagePath, camParams.username, camParams.password)
// const testImg = path.resolve("images", "test.png")

// POST request with image to ocr
const bodyFormData = new FormData();
bodyFormData.append('image', fs.createReadStream(imagePath));

const response = await axios.post(OCR_URL, bodyFormData, {
headers: {
...bodyFormData.getHeaders()
}
})

// delete image
fs.unlink(imagePath, (err) => {
if (err) {
// TODO: Critical logger setup
console.error(err)
}
})

return getSanitizedData(response.data.data)
const extractData = async (camParams, bedId) => {
const coordinates = await getMonitorCoordinates(bedId);
await CameraUtils.absoluteMove({ camParams, ...coordinates });

const snapshotUrl = await CameraUtils.getSnapshotUri({ camParams });

const fileName = "image-" + new Date().getTime() + ".jpeg";
const imagePath = path.resolve("images", fileName);
await downloadImage(
snapshotUrl.uri,
imagePath,
camParams.username,
camParams.password
);
// const testImg = path.resolve("images", "test.png")

// POST request with image to ocr
const bodyFormData = new FormData();
bodyFormData.append("image", fs.createReadStream(imagePath));

const response = await axios.post(OCR_URL, bodyFormData, {
headers: {
...bodyFormData.getHeaders(),
},
});

// delete image
fs.unlink(imagePath, (err) => {
if (err) {
// TODO: Critical logger setup
console.error(err);
}
});

}
return getSanitizedData(response.data.data);
};


const _getCamParams = (params) => {
Expand All @@ -109,26 +112,23 @@ const _getCamParams = (params) => {
return camParams;
};

export const updateObservationAuto = async (cameraParams, patientId)=>
{
try{
const cameraParamsSanitized = _getCamParams(cameraParams)
export const updateObservationAuto = async (cameraParams, bedId) => {
try {
const cameraParamsSanitized = _getCamParams(cameraParams);

const payload = await extractData(cameraParamsSanitized, patientId)
const payload = await extractData(cameraParamsSanitized, bedId);

return payload
}
catch(err)
{
console.log(err)
return payload;
} catch (err) {
console.log(err);
return {
"spo2": null,
"ventilator_spo2": null,
"resp": null,
"pulse": null,
"temperature": null,
"bp": null
}
spo2: null,
ventilator_spo2: null,
resp: null,
pulse: null,
temperature: null,
bp: null,
};
}
}
};

52 changes: 22 additions & 30 deletions src/automation/helper/getMonitorCoordinates.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,37 @@ import { PrismaClient } from "@prisma/client"

const prisma = new PrismaClient()

export const getMonitorCoordinates = async (patientId) => {

try{
const bed = await prisma.bed.findFirst({
export const getMonitorCoordinates = async (bedId) => {
try {
const preset = await prisma.preset.findFirst({
where: {
patientExternalId: {
equals: patientId
},
deleted: {
equals: false
}
}
})
bedId,
deleted: false,
},
});

if(!bed)
{
if (!preset) {
console.log(`Preset for bed ${bedId} not found`);

console.log("Bed not found")
return {
x: 0,
y: 0,
zoom: 0
}
zoom: 0,
};
}

return {
x: bed.x,
y: bed.y,
zoom: bed.zoom
}
}
catch(err)
{
console.log(err?.response?.data)
x: preset.x,
y: preset.y,
zoom: preset.zoom,
};
} catch (err) {
console.log(err?.response?.data);

return {
x: 0,
y: 0,
zoom: 0
}
x: 0,
y: 0,
zoom: 0,
};
}
}
};
Loading

0 comments on commit 5c013d4

Please sign in to comment.