Skip to content

Commit

Permalink
refactor and add basic test
Browse files Browse the repository at this point in the history
  • Loading branch information
gaoyi committed Oct 16, 2023
1 parent 5dbc014 commit 87bfd2b
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 123 deletions.
14 changes: 0 additions & 14 deletions Package.resolved

This file was deleted.

13 changes: 10 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@ import PackageDescription

let package = Package(
name: "swift_coco",
dependencies: [
.package(url: "https://github.com/google/flatbuffers.git", from: "23.5.26"),
],
// dependencies: [
// .package(url: "https://github.com/google/flatbuffers.git", from: "23.5.26"),
// ],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.executableTarget(
name: "swift_coco",
path: "Sources"),
.testTarget(
name: "MyTests",
dependencies: ["swift_coco"]
// sources: [
// "Tests/MyTests", // Add the path to your test files here
// ]
)
]
)
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
# swift_coco
# swift_coco

## Package used?
swift package [SwiftyJson](https://swiftpackageindex.com/SwiftyJSON/SwiftyJSON) not compitable with Linux for now, so write entirely using swift native [JSONDecoder](https://developer.apple.com/documentation/foundation/jsondecoder).
143 changes: 95 additions & 48 deletions Sources/Models/axera_anno.swift
Original file line number Diff line number Diff line change
@@ -1,63 +1,110 @@
protocol axera_shape {
var shapeType : String { get }
}
struct axera_frame_frame: Codable {
var imageUrl: String
var frameIndex: Int?
var valid: Bool
var imageWidth: Int
var imageHeight: Int
var rotation: Double
// deal with dynamic defined attr
var attributes: [String: String]?

struct axera_rect : Codable {
var x : Int
var y : Int
var width : Int
var height : Int
var rotation : Int
var points : [[String: Any]]
enum CodingKeys: String, CodingKey {
case imageUrl, frameIndex, valid, imageWidth, imageHeight, rotation
case attributes
}
}

struct axera_frame_frame : Codable {
var frameIndex : Int
var valid : Bool
// TODO variable length attributes
struct axera_frame: Codable {
var camera: String
var frames: [axera_frame_frame]
}

struct axera_frame : Codable {
var camera : String
var frames : [axera_frame_frame]
}
struct axera_camera_frame : Codable {
var frameIndex : Int
var isKeyFrame : Bool
var shapeType : String
// TODO : generic to deal with different shape
var shape : axera_shape
var order : Int
var attributes : [String: Any]
var isOCR : Bool
var OCRText : String
struct axera_camera_frame: Codable {
var frameIndex: Int?
var isKeyFrame: Bool?
var shapeType: String?
// TODO: enum to deal with different shape
var shape: BaseShape?
var order: Int?
var attributes: [String: String]?
var isOCR: Bool?
var isFormula: Bool?
var OCRText: String?

enum CodingKeys: String, CodingKey {
case shapeType
case shape
}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

shapeType = try container.decode(String.self, forKey: .shapeType)

//print the type of BaseShape

switch shapeType {
// TODO
case "rectangle":
let rect = try container.decode(Rectangle.self, forKey: .shape)
shape = .rectangle(rect)
case "polygon":
let poly = try container.decode(Polygon.self, forKey: .shape)
shape = .polygon(poly)
case "line":
let line = try container.decode(Line.self, forKey: .shape)
shape = .line(line)
case "ellipse":
let elli = try container.decode(Ellipse.self, forKey: .shape)
shape = .ellipse(elli)
case "dot":
let dot = try container.decode(Dot.self, forKey: .shape)
shape = .dot(dot)
case "cuboid":
let cub = try container.decode(Cuboid.self, forKey: .shape)
shape = .cuboid(cub)
case "l_shape":
let l_shape = try container.decode(L_shape.self, forKey: .shape)
shape = .l_shape(l_shape)
case "grid":
let grid = try container.decode(Grid.self, forKey: .shape)
shape = .grid(grid)
default:
shape = nil
}
}
}

struct axera_camera : Codable {
var camera : String
var frames : [axera_camera_frame]
struct axera_camera: Codable {
var camera: String
var frames: [axera_camera_frame]
}

struct axera_child : Codable {
var id : String
var name : String
var number : Int
var cameras : [axera_camera]
struct axera_child: Codable {
var id: String
var name: String
var displayName: String
var displayColor: String
var number: Int
var cameras: [axera_camera]
}

struct axera_instance : Codable {
var id : String
var category : String
var number : Int
var attributes : [String: Any]
var children : [axera_child]
struct axera_instance: Codable {
var id: String
var category: String
var categoryName: String
var number: Int
var attributes: [String: String]?
var children: [axera_child]
}

// defines axera shapes

struct axera_img_anno : Codable {
var auditId : String
var instances : [axera_instance]
var frames : [axera_frame]
var statistics : String
}
struct axera_img_anno: Codable {
var auditId: String
var instances: [axera_instance]
var frames: [axera_frame]
var relationships: [[String: String]]
var attributes: [String: String]?
var statistics: String
}
80 changes: 80 additions & 0 deletions Sources/Models/axera_shape.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
enum BaseShape: Codable {
case rectangle(Rectangle)
case polygon(Polygon)
case line(Line)
case ellipse(Ellipse)
case dot(Dot)
case cuboid(Cuboid)
case l_shape(L_shape)
case grid(Grid)
}

struct Rectangle_points: Codable {
var x: Double
var y: Double
}

struct Rectangle: Codable {
var x: Double
var y: Double
var width: Double
var height: Double
var area: Double
var rotation: Double
var points: [Rectangle_points]
var center: Double?
}

struct UserData: Codable {
var start: Bool?
var end: Bool?
}

struct Polygon_points: Codable {
var x: Double
var y: Double
var userData: UserData?
}

struct Polygon: Codable {
var points: [Polygon_points]
}

struct Line: Codable {
var points: [Polygon_points]
}

struct Ellipse: Codable {
var x: Double
var y: Double
var halfWidth: Double
var halfHeight: Double
}

struct Dot: Codable {
var x: Double
var y: Double
}

struct CuboidFrontBack: Codable {
var x: Double
var y: Double
var width: Double
var height: Double
}

struct Cuboid: Codable {
var front: CuboidFrontBack
var back: CuboidFrontBack
}

struct L_shape: Codable {
var front: CuboidFrontBack
var sidePoints: [Rectangle_points]
var center: Double
}

struct Grid: Codable {
var cols: [[String: Double]]
var rows: [[String: Double]]
}
21 changes: 17 additions & 4 deletions Sources/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,26 @@ import Foundation

// define the path to input json files
let jsonsURL = URL(fileURLWithPath: "/data/dataset/aXcellent/manu-label/obstacle/ANNOTATION_roadmark/FRONT_rect/")

let imageURL = URL(fileURLWithPath: "/data/dataset/aXcellent/manu-label/obstacle/IMAGE/FRONT_rect")
// define the path to output json files
let cocoURL = URL(fileURLWithPath: "/coco/gaoyi_dataset/coco/aXcellent_roadmark_FRONT_rect/annotations")
let cocoURL = URL(fileURLWithPath: "/coco/gaoyi_dataset/coco/aXcellent_roadmark_FRONT_rect/annotations/all.json")

// create the output coco directory
// create the output coco directory and file
do {
try FileManager.default.createDirectory(at: cocoURL, withIntermediateDirectories: true)
try FileManager.default.createDirectory(at: cocoURL, withIntermediateDirectories: true, attributes: nil)
} catch {
print(error)
}

// iter through jsonsURL to read json files
let jsons = try FileManager.default.contentsOfDirectory(at: jsonsURL, includingPropertiesForKeys: nil, options: .skipsSubdirectoryDescendants)
let sortedJSONs = jsons.sorted { (url1, url2) -> Bool in
return url1.lastPathComponent < url2.lastPathComponent
}
for (index, json) in sortedJSONs.enumerated() {
let coco_json = try! String(contentsOf: json)
print(coco_json)
if (index == 1) {
break
}
}
53 changes: 0 additions & 53 deletions Tests/MyTests.swift

This file was deleted.

Loading

0 comments on commit 87bfd2b

Please sign in to comment.