Skip to content

Commit

Permalink
Merge pull request #6 from notV3NOM/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
notV3NOM authored Jan 29, 2024
2 parents 4595541 + ab9888b commit ec5c7b8
Show file tree
Hide file tree
Showing 7 changed files with 1,353 additions and 23 deletions.
7 changes: 2 additions & 5 deletions apps/frontend/src/app/PageArtifact/ArtifactEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ import SubstatInput from './ArtifactEditor/Components/SubstatInput'
import UploadExplainationModal from './ArtifactEditor/Components/UploadExplainationModal'
import { textsFromImage } from './ScanningUtil'
import { LocationAutocomplete } from '../Components/Character/LocationAutocomplete'
import { shouldShowDevComponents } from '../Util/Util'

const allSubstatFilter = new Set(allSubstatKeys)
type ResetMessage = { type: 'reset' }
Expand Down Expand Up @@ -144,9 +143,7 @@ export default function ArtifactEditor({
disableSet = false,
disableSlot = false,
}: ArtifactEditorProps) {
const queueRef = useRef(
new ScanningQueue(textsFromImage, shouldShowDevComponents)
)
const queueRef = useRef(new ScanningQueue(textsFromImage, true))
const queue = queueRef.current
const { t } = useTranslation('artifact')

Expand Down Expand Up @@ -594,7 +591,7 @@ export default function ArtifactEditor({
</Button>
</label>
</Grid>
{shouldShowDevComponents && debugImgs && (
{debugImgs && (
<Grid item>
<DebugModal imgs={debugImgs} />
</Grid>
Expand Down
32 changes: 28 additions & 4 deletions libs/gi-art-scanner/src/lib/artifactBoxPredictor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ export function artifactBoxPredictor(
): artifactPredictorResult {
const debugImgs = {} as Record<string, string>

imageData =
imageData.width > imageData.height
? boxPredictor(imageData, debugImgs)
: imageData
imageData = boxPredictor(convertToLandscape(imageData), debugImgs)

return {
artifactImageData: imageData,
Expand Down Expand Up @@ -129,3 +126,30 @@ function findLargestRectangle(imageData: ImageData): Rectangle {

return maxRectangle
}

function convertToLandscape(inputImageData: ImageData): ImageData {
const originalWidth = inputImageData.width
const originalHeight = inputImageData.height

// Check if the image is already landscape
if (originalWidth > originalHeight) {
return inputImageData
}

const enlargedWidth = Math.max(originalWidth, originalHeight) + 20
const enlargedHeight = Math.max(originalWidth, originalHeight) + 20
const canvas = document.createElement('canvas')
canvas.width = enlargedWidth
canvas.height = enlargedHeight
const ctx = canvas.getContext('2d')!

const xPadding = Math.floor((enlargedWidth - originalWidth) / 2)
const yPadding = Math.floor((enlargedHeight - originalHeight) / 2)

ctx.fillStyle = 'black'
ctx.fillRect(0, 0, enlargedWidth, enlargedHeight)

ctx.putImageData(inputImageData, xPadding, yPadding)

return ctx.getImageData(0, 0, enlargedWidth, enlargedHeight)
}
4 changes: 3 additions & 1 deletion libs/gi-art-scanner/src/lib/artifactPredictor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
crop,
edgeDetection,
extractBox,
findGreenSplitHeight,
findSplitHeight,
imageDataToCanvas,
scaleImage,
Expand Down Expand Up @@ -48,9 +49,10 @@ export async function artifactPredictor(
headerCard,
artifactNameHeaderRatio * headerCard.height
)
const greenSplitHeight = findGreenSplitHeight(whiteCard, 20)
const [ArtifactSubstats, ArtifactSetLocation] = splitImageVertical(
whiteCard,
ArtifactMainStatCard.height
greenSplitHeight - ArtifactNameCard.height / 4
)
const [ArtifactSet, ArtifactLocation] = splitImageVertical(
ArtifactSetLocation,
Expand Down
31 changes: 19 additions & 12 deletions libs/gi-art-scanner/src/lib/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,10 @@ export function parseSetKeys(texts: string[]): Set<ArtifactSetKey> {
const kdist: Array<KeyDist<ArtifactSetKey>> = []
for (const text of texts)
for (const key of allArtifactSetKeys)
kdist.push([
key,
levenshteinDistance(
text.replace(/\W/g, ''),
key //TODO: use the translated set name?
),
])
return getBestKeyDist(kdist)
kdist.push([key, levenshteinDistance(text.replace(/\W/g, ''), key)])
const bestMatch = getBestKeyDist(kdist)
console.log('Best Match for Set ', bestMatch)
return bestMatch
}

export function parseSlotKeys(texts: string[]): Set<ArtifactSlotKey> {
Expand All @@ -48,15 +44,18 @@ export function parseSlotKeys(texts: string[]): Set<ArtifactSlotKey> {
artSlotNames[key].replace(/\W/g, '')
),
])
return getBestKeyDist(kdist)
const bestMatch = getBestKeyDist(kdist)
console.log('Best Match for Slot ', bestMatch)
return bestMatch
}

export function parseMainStatKeys(texts: string[]): Set<MainStatKey> {
const kdist: Array<KeyDist<MainStatKey>> = []
for (const text of texts)
for (const key of allMainStatKeys) {
const statStr = statMap[key]?.toLowerCase()
if (statStr.length <= 3) {
if (text.toLowerCase().includes(statStr ?? '')) kdist.push([key, 0])
if (text.toLowerCase().includes(statStr)) kdist.push([key, 0])
} else
kdist.push([
key,
Expand All @@ -66,13 +65,18 @@ export function parseMainStatKeys(texts: string[]): Set<MainStatKey> {
),
])
}
return getBestKeyDist(kdist)
const bestMatch = getBestKeyDist(kdist)
console.log('Best Match for Main Stat ', bestMatch)
return bestMatch
}

export function parseMainStatValues(
texts: string[]
): { mainStatValue: number; unit?: string }[] {
const results: { mainStatValue: number; unit?: string }[] = []
for (const text of texts) {
for (let text of texts) {
//We know that it will be a number with , or % or .
text = text.replace(/[^0-9%,.]/g, '1')
let regex = /(\d+[,|\\.]+\d)%/
let match = regex.exec(text)
if (match)
Expand All @@ -89,6 +93,7 @@ export function parseMainStatValues(
mainStatValue: parseInt(match[1].replace(/[,|\\.]+/g, '')),
})
}
console.log('Best Match for Main Stat Value ', results)
return results
}

Expand All @@ -113,6 +118,7 @@ export function parseSubstats(texts: string[]): ISubstat[] {
})
})
}
console.log('Best Match for Substats ', matches.slice(0, 4))
return matches.slice(0, 4)
}

Expand All @@ -137,5 +143,6 @@ export function parseLocation(texts: string[]): LocationCharacterKey {
// traveler is the default value when we don't recognize the name
kdist.push(['Traveler', 8])
const [char] = getBestKeyDist(kdist)
console.log('Best Match for Equipped ', char)
return char
}
2 changes: 1 addition & 1 deletion libs/gi-art-scanner/src/lib/processImg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export async function processEntry(
textsFromImage
)

if (debug) console.log(prediction)
if (debug) console.log('OCR Prediction', prediction)

const equipHistogram = histogramContAnalysis(
artifactImageData,
Expand Down
Loading

0 comments on commit ec5c7b8

Please sign in to comment.