Skip to content

Commit

Permalink
Merge pull request #2 from CS-Tao/master
Browse files Browse the repository at this point in the history
修复无车辆时的 bug
  • Loading branch information
lsq210 authored Dec 1, 2022
2 parents b47ba47 + 7157894 commit 4043457
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 34 deletions.
15 changes: 11 additions & 4 deletions GTAVControler/Automation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@ namespace GTAVControler
{
public class Automation
{
private static GTAVUtils.ROI[] GetRoIs(int width, int height)
private static GTAVUtils.ImageInfo GetImageInfo(Bitmap screenshot)
{
Vector3 camPos = World.RenderingCamera.Position;
Vector3 camRot = World.RenderingCamera.Rotation;
return new GTAVUtils.ImageInfo(screenshot.Width, screenshot.Height, camPos, camRot);
}

private static GTAVUtils.ROI[] GetRoIs(GTAVUtils.ImageInfo imageInfo)
{
Vehicle[] vehicles = World.GetAllVehicles();
List<GTAVUtils.ROI> rois = new List<GTAVUtils.ROI>();
foreach (Vehicle vehicle in vehicles)
{
GTAVUtils.ROI roi = new GTAVUtils.ROI(vehicle, (GTAVUtils.ROI.DetectionType)vehicle.ClassType, vehicle.Model.IsBigVehicle,rois.Count, width, height, camPos, camRot);
GTAVUtils.ROI roi = new GTAVUtils.ROI(vehicle, (GTAVUtils.ROI.DetectionType)vehicle.ClassType, vehicle.Model.IsBigVehicle, rois.Count, imageInfo);
if (roi.BBox.IsValid)
{
rois.Add(roi);
Expand All @@ -38,11 +43,13 @@ public static void SaveGTAVData()
// screenshot
Bitmap screenshot = GTAVUtils.Common.GetScreenshot();

GTAVUtils.ImageInfo imageInfo = GetImageInfo(screenshot);

// roilabel
GTAVUtils.ROI[] rois = GetRoIs(screenshot.Width, screenshot.Height);
GTAVUtils.ROI[] rois = GetRoIs(imageInfo);

// preprocess and save data
GTAVUtils.Common.DataPreprocess(screenshot, rois).Save(timestamp, timestamp);
GTAVUtils.Common.DataPreprocess(screenshot, rois, imageInfo).Save(timestamp, timestamp);
}

public static void Pause()
Expand Down
18 changes: 10 additions & 8 deletions GTAVUtils/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using GTA.Math;

namespace GTAVUtils
{
Expand All @@ -16,7 +17,7 @@ public static Bitmap GetScreenshot()
return screenshot;
}

public static GTAVData DataPreprocess(Bitmap screenshot, ROI[] RoIs)
public static GTAVData DataPreprocess(Bitmap screenshot, ROI[] RoIs, ImageInfo imageInfo)
{
float cutBorderWidth = .1f;

Expand All @@ -25,17 +26,18 @@ public static GTAVData DataPreprocess(Bitmap screenshot, ROI[] RoIs)
int cutHeight = (int)(screenshot.Height * cutBorderWidth);
Rectangle rect = new Rectangle(cutWidth, cutHeight, screenshot.Width - 2 * cutWidth, screenshot.Height - 2 * cutHeight);
Bitmap cutedScreenshot = screenshot.Clone(rect, System.Drawing.Imaging.PixelFormat.DontCare);
ImageInfo cutedImageInfo = new ImageInfo(imageInfo)
{
Width = cutedScreenshot.Width,
Height = cutedScreenshot.Height
};

// filterRoIs
List<ROI> filteredRoIs = new List<ROI>();
foreach (ROI roi in RoIs)
{
ROI filteredRoI = new ROI(roi)
{
ImageWidth = cutedScreenshot.Width,
ImageHeight = cutedScreenshot.Height
};
float ratio = roi.ImageWidth / (float)filteredRoI.ImageWidth;
ROI filteredRoI = new ROI(roi);
float ratio = imageInfo.Width / (float)cutedImageInfo.Width;
if (roi.BBox.Quality != GTABoundingBox2.DataQuality.Low)
{
if (roi.BBox.Min.X > cutBorderWidth && roi.BBox.Min.Y > cutBorderWidth)
Expand All @@ -49,7 +51,7 @@ public static GTAVData DataPreprocess(Bitmap screenshot, ROI[] RoIs)
}
}
}
return new GTAVData(cutedScreenshot, filteredRoIs.ToArray());
return new GTAVData(cutedScreenshot, filteredRoIs.ToArray(), imageInfo);
}
}
}
61 changes: 39 additions & 22 deletions GTAVUtils/DataStructures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public static GTABoundingBox2 ComputeBoundingBox(Entity entity)

public class ROI
{
public ROI(Entity entity, DetectionType detectionType, bool isBigVehicle, int order, int imageWidth, int imageHeight, Vector3 camPos, Vector3 camRot)
public ROI(Entity entity, DetectionType detectionType, bool isBigVehicle, int order, ImageInfo imageInfo)
{
RoIEntity = entity;
Pos = new Vector3(entity.Position.X, entity.Position.Y, entity.Position.Z);
Expand All @@ -112,10 +112,7 @@ public ROI(Entity entity, DetectionType detectionType, bool isBigVehicle, int or
Type = detectionType;
IsBigVehicle = isBigVehicle;
Order = order;
ImageWidth = imageWidth;
ImageHeight = imageHeight;
CamPos = camPos;
CamRot = camRot;
ImageInfo = imageInfo;
}

public ROI(ROI preROI)
Expand All @@ -126,10 +123,7 @@ public ROI(ROI preROI)
Type = preROI.Type;
IsBigVehicle = preROI.IsBigVehicle;
Order = preROI.Order;
ImageWidth = preROI.ImageWidth;
ImageHeight = preROI.ImageHeight;
CamPos = preROI.CamPos;
CamRot = preROI.CamRot;
ImageInfo = preROI.ImageInfo;
}

public enum DetectionType
Expand Down Expand Up @@ -169,25 +163,19 @@ public enum DetectionType

public bool IsBigVehicle { get; }

public int ImageWidth { get; set; }

public int ImageHeight { get; set; }

public Vector3 CamPos { get; }

public Vector3 CamRot { get; }
public int Order { get; }

public int Order { get; }
public ImageInfo ImageInfo { get; }

// FIXME:
private int GetWidth(float w)
{
return (int)(w * ImageWidth);
return (int)(w * ImageInfo.Width);
}
private int GetHeight(float h)
{
return (int)(h * ImageHeight);
return (int)(h * ImageInfo.Height);
}

public bool CheckVisible()
{
Vector3 cameraPos = World.RenderingCamera.Position;
Expand Down Expand Up @@ -239,13 +227,41 @@ public void Draw(Bitmap image)
}
}

public class ImageInfo
{
public ImageInfo(int width, int height, Vector3 camPos, Vector3 camRot)
{
Width = width;
Height = height;
CamPos = camPos;
CamRot = camRot;
}

public ImageInfo(ImageInfo preImageInfo)
{
Width = preImageInfo.Width;
Height = preImageInfo.Height;
CamPos = preImageInfo.CamPos;
CamRot = preImageInfo.CamRot;
}

public int Width { get; set; }

public int Height { get; set; }

public Vector3 CamPos { get; }

public Vector3 CamRot { get; }
}

public class GTAVData
{
public GTAVData(Bitmap image, ROI[] rois)
public GTAVData(Bitmap image, ROI[] rois, ImageInfo imageInfo)
{
Version = "v0.0.1";
Image = image;
RoIs = rois;
ImageInfo = imageInfo;
}

public string Version { get; set; }
Expand All @@ -267,12 +283,13 @@ public int ImageHight
}

public ROI[] RoIs;
public ImageInfo ImageInfo;

public void Save(string imageName, string labelName, bool drawBBox = true)
{
GTAVManager.SaveImage(imageName, Image);
string imageSize = $"{Image.Width},{Image.Height}";
string camInfo = $"{RoIs[0].CamPos},{RoIs[0].CamRot}";
string camInfo = $"{ImageInfo.CamPos},{ImageInfo.CamRot}";
string txt = $"{imageSize}\n{camInfo}\n";
for (int i = 0; i < RoIs.Length; i++)
{
Expand Down

0 comments on commit 4043457

Please sign in to comment.