Skip to content

Commit

Permalink
Merge pull request #4 from notV3NOM/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
notV3NOM committed Jan 28, 2024
2 parents 92f0f15 + 70f566c commit a2cb7f6
Show file tree
Hide file tree
Showing 9 changed files with 692 additions and 332 deletions.
7 changes: 6 additions & 1 deletion apps/frontend/src/app/Components/Character/CharacterCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type CharacterCardProps = {
characterChildren?: Displayable
footer?: Displayable
hideStats?: boolean
hideArtifacts?: boolean
isTeammateCard?: boolean
}
export default function CharacterCard({
Expand All @@ -69,6 +70,7 @@ export default function CharacterCard({
onClickTeammate,
footer,
hideStats,
hideArtifacts,
isTeammateCard,
}: CharacterCardProps) {
const { database } = useContext(DatabaseContext)
Expand Down Expand Up @@ -157,6 +159,7 @@ export default function CharacterCard({
character={character}
onClickTeammate={onClickTeammate}
hideStats={hideStats}
hideArtifacts={hideArtifacts}
weaponChildren={weaponChildren}
artifactChildren={artifactChildren}
characterChildren={characterChildren}
Expand All @@ -181,6 +184,7 @@ type ExistingCharacterCardContentProps = {
character: ICachedCharacter
onClickTeammate?: (characterKey: CharacterKey) => void
hideStats?: boolean
hideArtifacts?: boolean
weaponChildren?: Displayable
artifactChildren?: Displayable
characterChildren?: Displayable
Expand All @@ -195,6 +199,7 @@ function ExistingCharacterCardContent({
character,
onClickTeammate,
hideStats,
hideArtifacts,
weaponChildren,
artifactChildren,
characterChildren,
Expand All @@ -218,7 +223,7 @@ function ExistingCharacterCardContent({
padding: hideStats ? `${theme.spacing(1)}!important` : undefined,
})}
>
<Artifacts />
{!hideArtifacts && <Artifacts />}
{!isTeammateCard && (
<Grid container columns={4} spacing={0.75}>
<Grid item xs={1} height="100%">
Expand Down
4 changes: 2 additions & 2 deletions apps/frontend/src/app/PageArtifact/ScanningUtil.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { BorrowManager } from '@genshin-optimizer/util'
import type { RecognizeResult, Scheduler } from 'tesseract.js'
import { createScheduler, createWorker } from 'tesseract.js'

const workerCount = 2
const workerCount = 20

const schedulers = new BorrowManager(
async (language): Promise<Scheduler> => {
Expand Down Expand Up @@ -32,7 +32,7 @@ export async function textsFromImage(
): Promise<string[]> {
const canvas = imageDataToCanvas(imageData)
const rec = await schedulers.borrow(
'genshin_fast_09_04_21',
'eng',
async (scheduler) =>
(await (
await scheduler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,21 +530,11 @@ export default function TabBuild() {
<CharacterCard
characterKey={characterKey}
onClickTeammate={onClickTeammate}
hideStats={true}
hideArtifacts={true}
/>
</Box>
<BonusStatsCard />
</Grid>

{/* 2 */}
<Grid
item
xs={12}
sm={6}
lg={4}
display="flex"
flexDirection="column"
gap={1}
>
{/* Level Filter */}
<CardLight>
<CardContent sx={{ display: 'flex', gap: 1 }}>
Expand All @@ -567,7 +557,37 @@ export default function TabBuild() {
/>
</CardContent>
</CardLight>
<CardContent>
<Box display="flex" alignItems="center" gap={1}>
<AssumeFullLevelToggle
mainStatAssumptionLevel={mainStatAssumptionLevel}
setmainStatAssumptionLevel={(
mainStatAssumptionLevel: number
) => buildSettingDispatch({ mainStatAssumptionLevel })}
disabled={generatingBuilds}
/>
<InfoTooltip
title={
<Box>
<Typography variant="h6">{t`mainStat.levelAssTooltip.title`}</Typography>
<Typography>{t`mainStat.levelAssTooltip.desc`}</Typography>
</Box>
}
/>
</Box>
</CardContent>
</Grid>

{/* 2 */}
<Grid
item
xs={12}
sm={6}
lg={4}
display="flex"
flexDirection="column"
gap={1}
>
{/* Main Stat Filters */}
<CardLight>
<CardContent>
Expand All @@ -576,25 +596,6 @@ export default function TabBuild() {
>{t`mainStat.title`}</Typography>
</CardContent>
<Divider />
<CardContent>
<Box display="flex" alignItems="center" gap={1}>
<AssumeFullLevelToggle
mainStatAssumptionLevel={mainStatAssumptionLevel}
setmainStatAssumptionLevel={(
mainStatAssumptionLevel: number
) => buildSettingDispatch({ mainStatAssumptionLevel })}
disabled={generatingBuilds}
/>
<InfoTooltip
title={
<Box>
<Typography variant="h6">{t`mainStat.levelAssTooltip.title`}</Typography>
<Typography>{t`mainStat.levelAssTooltip.desc`}</Typography>
</Box>
}
/>
</Box>
</CardContent>
{/* main stat selector */}
<MainStatSelectionCard
disabled={generatingBuilds}
Expand Down Expand Up @@ -645,6 +646,7 @@ export default function TabBuild() {
<StatFilterCard disabled={generatingBuilds} />
</Grid>
</Grid>
<br />
{/* Footer */}
{isSM && targetSelector}
<ButtonGroup>
Expand Down
1 change: 0 additions & 1 deletion apps/frontend/src/app/PageHome/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
useTheme,
} from '@mui/material'
import ReactGA from 'react-ga4'
import { Trans, useTranslation } from 'react-i18next'
import CardDark from '../Components/Card/CardDark'
import InventoryCard from './InventoryCard'

Expand Down
Binary file added apps/frontend/src/assets/eng.traineddata.gz
Binary file not shown.
131 changes: 131 additions & 0 deletions libs/gi-art-scanner/src/lib/artifactBoxPredictor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import {
convertToBlackAndWhite,
edgeDetection,
imageDataToCanvas,
} from '@genshin-optimizer/img-util'

interface Point {
x: number
y: number
}
interface Rectangle {
topLeft: Point
bottomRight: Point
}
type artifactPredictorResult = {
artifactImageData: ImageData
debugImgs: Record<string, string>
}

export function artifactBoxPredictor(
imageData: ImageData
): artifactPredictorResult {
const debugImgs = {} as Record<string, string>

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

return {
artifactImageData: imageData,
debugImgs: debugImgs,
}
}

function boxPredictor(
imageData: ImageData,
debugImgs: Record<string, string>
): ImageData {
const edgeDetectedImageData = edgeDetection(imageData)

debugImgs['Edge Detection Full Screen'] = imageDataToCanvas(
edgeDetectedImageData
).toDataURL()

const bwEdgeData = convertToBlackAndWhite(
new ImageData(
new Uint8ClampedArray(edgeDetectedImageData.data),
edgeDetectedImageData.width,
edgeDetectedImageData.height
)
)

const { topLeft, bottomRight } = findLargestRectangle(bwEdgeData)

const canvas = document.createElement('canvas')
const context = canvas.getContext('2d')!
const width = bottomRight.x - topLeft.x + 1
const height = bottomRight.y - topLeft.y + 1

canvas.width = width
canvas.height = height

context.drawImage(
imageDataToCanvas(imageData),
topLeft.x,
topLeft.y,
width,
height,
0,
0,
width,
height
)

const largestRectangleImageData = context.getImageData(0, 0, width, height)
debugImgs['Largest Rectangle'] = canvas.toDataURL()

return largestRectangleImageData
}

function findLargestRectangle(imageData: ImageData): Rectangle {
const width = imageData.width
const height = imageData.height
const data = imageData.data

let maxArea = 0
let maxRectangle: Rectangle = {
topLeft: { x: 0, y: 0 },
bottomRight: { x: 0, y: 0 },
}

function isWhite(x: number, y: number): boolean {
const pixelIndex = (y * width + x) * 4
return data[pixelIndex] === 255
}

function expandFromPoint(x: number, y: number): Rectangle {
let left = x
let right = x
let top = y
let bottom = y

while (left > 0 && isWhite(left - 1, y)) left--
while (right < width - 1 && isWhite(right + 1, y)) right++
while (top > 0 && isWhite(x, top - 1)) top--
while (bottom < height - 1 && isWhite(x, bottom + 1)) bottom++

return {
topLeft: { x: left, y: top },
bottomRight: { x: right, y: bottom },
}
}

for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
if (isWhite(x, y)) {
const rectangle = expandFromPoint(x, y)
const area =
(rectangle.bottomRight.x - rectangle.topLeft.x + 1) *
(rectangle.bottomRight.y - rectangle.topLeft.y + 1)
if (area > maxArea) {
maxArea = area
maxRectangle = rectangle
}
}
}
}

return maxRectangle
}
Loading

0 comments on commit a2cb7f6

Please sign in to comment.